Symfony2: Why is the view annotation in NelmioApiDocBundle not working? - symfony

I want to split up my documentation for the api I am building. I am using NelmioApiDocBundle and they have a perfect way with the view annotation. Nelmio view The problem is my method stays in default view and not in the suggested oauth view:
So /doc/api/oauth/ or /api/doc/oauth ends in 404
//config.yml
# app/config/config.yml
nelmio_api_doc: ~
// app/config/routing.yml
NelmioApiDocBundle:
resource: "#NelmioApiDocBundle/Resources/config/routing.yml"
prefix: /api/doc
// routing.yml
profile_rest_oauth:
resource: "#ProfileBundle/Rest/Oauth/RestController.php"
type: rest
prefix: /api/oauth
profile_rest:
resource: "#ProfileBundle/Rest/Xwsse/RestController.php"
type: rest
prefix: /api
//RestController
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpFoundation\Request;
class RestController extends FOSRestController
{
/**
* #ApiDoc(
* description="Update profile for user",
* section="profile",
* https=true,
* statusCodes={
* 200="OK, user profile updated",
* 400="Wrong input, no update"
* },
* views = { "oauth" }
* )
*/
public function putProfileAction(Request $request)
{
}
//composer.json
"nelmio/api-doc-bundle": "2.7.0",

To answer my own question: the version "2.7.0" in not compatible with the view parameter you need atleast 2.9.0". It's hard to understand it from the symfony documentation because it show version 2.x symfony documentation

Looks good man. At least i can say if you set up everything right - the /api/doc/oauth should never give you 404.
Try to change both your
resource: "#ProfileBundle/Rest/Xwsse/RestController.php"
to this like
resource: "#ProfileBundle/Resources/config/routing.yml"

Related

Symfony - Route 404

I have a Symfony (4.1) project. In this project i need to create new route, like this.
Controller
/**
*
* #Route("/search-user", name="search-user")
*/
public function searchUser(Request $request)
{
dd("Search user");
return $this->render("XXX");
}
Routes.yaml
controllers:
resource: '../src/Controller/'
type: annotation
prefix: '/'
The problem: When i go to endpoint/search-user returns 404 not found.
I´m some new in Symfony, so somebody can see what's wrong?
You can launch
php bin/console debug:router
to have a list of all your existing routing.
Can you add your controller declaration?
check that is in the src/Controller folder
check that there is no routing prefix in your controller (#route before class declaration)

User password not encrypted when using FOSUserBundle + EasyAdminBundle with Symfony 3.4

I use Symfony 3.4 with FOSUserBundle and EasyAdminBundle.
I've been stuck for a while on the following problem: when I create a new user via EasyAdmin, the password entered is not hashed, it remains clear in the database and in the edit form of the created user (in EasyAdmin), while there is no problem when I create a user via the form generated by FOSUserBundle (register).
My User entity :
<?php
// src/Repas/UserBundle/Entity/User.php
namespace Repas\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
}
My AdminController.php file :
<?php
namespace Repas\MenusBundle\Controller;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AdminController as BaseAdminController;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
class AdminController extends BaseAdminController
{
public function createNewUserEntity()
{
return $this->get('fos_user.user_manager')->createUser();
}
public function persistUserEntity($user)
{
$this->get('fos_user.user_manager')->updateUser($user, false);
parent::persistEntity($user);
}
public function updateUserEntity($user)
{
$this->get('fos_user.user_manager')->updateUser($user, false);
parent::updateEntity($user);
}
}
In my config.yml file :
easy_admin:
entities:
User:
class: Repas\UserBundle\Entity\User
export_path: '%kernel.root_dir/../var/export/user'
password_encoding: { algorithm: 'bcrypt', cost: 12 }
In my security.yml file :
encoders:
Repas\UserBundle\Entity\User: bcrypt
In my routing.yml file :
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.xml"
easy_admin_bundle:
resource: "#RepasMenusBundle/Controller/AdminController.php"
type: annotation
prefix: /admin
I've been through many forums, as well as the official docs, I think I followed everything properly but I certainly missed something.
Thank you for your help.
The EasyAdminBundle doesn't define an option to encrypt the password, it only provides options to save the entities (a crud) which you can extend by defining custom actions based on routes or actions inside an overrided AdminController in order to integrate with FOSUserBundle.
Example
easy_admin:
entities:
User:
list:
actions:
- { name: 'create_user', type: 'route' } //or nothing on type to use the action option
At this point you already have either a defined controller accessible by route or an overriden controller which handles the specified User actions. You only need to use the FOSUser methods to encrypt the password properly, as read in this doc.
Hope it helps!
Ok, I guess my mistake is that in the form generated by Easyadmin to create a new user, Easyadmin generates a field named "password" instead of "plainPassword" which is the one FOSUser uses to encrypt the entered password. So I think I just have to create a new "plainPassword" field in my "Easyadmin new user" form and enter the user password in that field to encrypt it. Then the encrypted password will be stored in "password" field of the database.
I will tell you if that is the solution.

Gedmo loggable working but not storing username

I have got logging working on my entity so that when I make a change to a product field with the #Gedmo\Versioned annotation a new version is created. However the only problem is that the username field remains NULL. There is an authenticated user as the update is performed in Sonata Admin backend.
<?php
namespace MyApp\CmsBundle\Entity\Log;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry;
use Gedmo\Loggable\Entity\Repository\LogEntryRepository;
/**
* #ORM\Entity(repositoryClass="Gedmo\Loggable\Entity\Repository\LogEntryRepository", readOnly=true)
* #ORM\Table(
* name="log_product",
* indexes={
* #ORM\Index(name="log_class_lookup_idx", columns={"object_class"}),
* #ORM\Index(name="log_date_lookup_idx", columns={"logged_at"}),
* #ORM\Index(name="log_user_lookup_idx", columns={"username"}),
* }
* )
*/
class ProductLog extends AbstractLogEntry
{
}
So it would appear the log_user_lookup_idx isn't working correctly, is there a particular bit of config I require for this?
It appears I was missing a bit of config, adding the following to the main app/config/config.yml file did the trick.
stof_doctrine_extensions:
default_locale: en
orm:
default:
loggable: true
I did originally have this in my bundles' services.yml config:
gedmo.listener.loggable:
class: Gedmo\Loggable\LoggableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ "#annotation_reader" ] ]
This managed to track the entity being modified but not the user, I have since removed this config and the logging remains to work with just the stof_doctrine_extensions config setting.
If you have both in your code base then everything will be logged twice I found.

How can I make routing case insensitive in Symfony2?

Is there any configuration in Symfony2 that allow use of case Insensitive routing?
For example, routes below should be treated as they are the same:
www.exmaple.com/all
www.exmaple.com/ALL
There is a pull request about this, but no reference how to make it happen.
As of Symfony2.4, you can now define condition to your route and use the expression language to do complexe check. See Router: Completely Customized Route Matching with Conditions documentation.
Another solution would be to override the route compiler class to change/extend the php compilation of your route matcher:
contact:
path: /{media}
defaults: { _controller: AcmeDemoBundle:Main:contact, media: organic }
requirements:
media: email|organic|ad
options:
compiler_class: MyVendor\Component\Routing\CaseInsensitiveRouteCompiler
See Symfony\Component\Routing\RouteCompiler class.
Or, as fabpot said in the pull request comment, your could override Request::getPathInfo[1] method to always return a lowercase string (use the setFactory[2] to override default request class).
*1 github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L866
*2 github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L402
I found a nifty way to do this in pure symfony (no apache mod_rewrite) without having to create a case-insensitive forwarding rule for every route.
This utilizes the twig ExceptionController. Because this occures after the routing has failed to match (or a 404 exception has been thrown for some other reason) it won't break any existing routing urls that use capitals (though that would still be a bad idea).
namespace Application\Symfony\TwigBundle\Controller;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController;
/**
* ExceptionController.
*/
class ExceptionController extends BaseExceptionController
{
/**
* Redirects 404s on urls with uppercase letters to the lowercase verion,
* then uses it's parent class to
* Convert an Exception to a Response.
*
* #param Request $request The request
* #param FlattenException $exception A FlattenException instance
* #param DebugLoggerInterface $logger A DebugLoggerInterface instance
*
* #return Response
*
* #throws \InvalidArgumentException When the exception template does not exist
*/
public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null)
{
if ( (string) $exception->getStatusCode() === '404' && preg_match('/[A-Z]/', $request->getPathInfo())) {
$lowercaseUrl = strtolower($request->getPathInfo());
if ($request->isMethod('POST')) {
return new RedirectResponse(
$lowercaseUrl,
'307'//307 status code preserves post information.
);
}
$queryString = $request->getQueryString();
return new RedirectResponse(
$lowercaseUrl.( strlen($queryString)>0 ? '?'.$queryString : '' ),
'301'//301 status code plays nice with search engines
);
}
return parent::showAction($request, $exception, $logger);
}
}
The only other trick is that you need to configure this controller as a service you can inject the correct arguments into the parent class's constructor:
in services.yml
services:
application.custom.exception_controller:
class: Application\Symfony\TwigBundle\Controller\ExceptionController
arguments: [ "#twig", "%kernel.debug%" ]
in config.yml:
twig:
exception_controller: application.custom.exception_controller:showAction
Of course, you can stick this controller and service definition anywhere
As far as I know, this isn't possible with Symfony 2. However, you should be able to accomplish it with Apache's mod_rewrite. See this blog post for details.
Make sure to read the comments, as there are some errors with the initial post.

No route found for PUT /api/users/id

I'm trying to update a record in my mysql database by calling the put method from my sencha touch 2 frontend. I'm calling this url /api/users/id but I keep getting a Symfony error:
No route found for "PUT /api/users/1
This is what I have in my routing.yml file
users:
resource: "Acme\MainBundle\Controller\UsersController"
prefix: /api
type: rest
Also, I have the putUsersAction setup in my User*s*Controller
public function putUsersAction($id, Request $request)
{
$values['birthdate'] = $request->get('birthdate');
$values['clubid'] = $request->get('clubid');
$em = $this->getDoctrine()->getEntityManager();
$user = $this->getDoctrine()
->getRepository('AcmeMainBundle:User')
->find($id);
$club = $this->getDoctrine()
->getRepository('AcmeMainBundle:Club')
->find($values['clubid']);
$user->setBirthdate($values['birthdate']);
$user->addClub($club);
$em->flush();
$view = View::create()
->setStatusCode(200)
->setData($user);
return $this->get('fos_rest.view_handler')->handle($view);
}
Why is Symfony telling me there's no PUT /api/users/id route?
EDIT 1: router:debug output
[router] Current routes
Name Method Pattern
_wdt ANY /_wdt/{token}
_profiler_search ANY /_profiler/search
_profiler_purge ANY /_profiler/purge
_profiler_info ANY /_profiler/info/{about}
_profiler_import ANY /_profiler/import
_profiler_export ANY /_profiler/export/{token}.txt
_profiler_phpinfo ANY /_profiler/phpinfo
_profiler_search_results ANY /_profiler/{token}/search/results
_profiler ANY /_profiler/{token}
_profiler_redirect ANY /_profiler/
_configurator_home ANY /_configurator/
_configurator_step ANY /_configurator/step/{index}
_configurator_final ANY /_configurator/final
get_users GET /api/users.{_format}
post_users POST /api/users.{_format}
get_clubs GET /api/clubs.{_format}
post_clubs POST /api/clubs.{_format}
put_users PUT /api/users.{_format}
At first you should debug your routing and see if the route is being registered correctly. Your posted routing snipped lacks correct intendation. It should read:
user:
resource: "Acme\MainBundle\Controller\UserController"
prefix: /api
type: rest
Afterwards you can debug your routing with the console command:
php app/console router:debug
Furthermore you can use grep ( Unix ) or findstr ( Windows ) to search the output for your route:
php app/console router:debug | grep /api
or
php app/console router:debug | findstr /api
Next make sure for the automatic routing of FOSRestBundle to work as expected to name your controller User*s*Controller and the file User*s*Controller.php.
See: FOSRestBundle Documentation
Please note that you have forgotten to call persist($user) before flushing and that you cannot call flush on your User entity but on your EntityManager. See my example below.
You can slim down your controller a lot by using DependencyInjection , the symfony2 ParamConverter, implicit resource name definition and the #View Annotation provided by the FOSRestBundle.
Your Controller would then read something like this:
<?php
namespace Acme\MainBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use JMS\DiExtraBundle\Annotation as DI;
use Acme\MainBundle\Entity\User;
use FOS\RestBundle\Controller\Annotations\View;
/**
* #DI\Service
*/
class UserController
{
/** #DI\Inject("doctrine.orm.entity_manager") */
private $em;
// ...
/**
* #View()
*/
public function putAction(User $user, Request $request)
{
$club = $this->em
->getRepository('AcmeMainBundle:Club')
->findOneById($request->get('clubid'));
$user
->setBirthdate($request->get('birthdate')
->addClub($club);
// you should add some validation here
$this->em->persist($user);
$this->em->flush();
return $user;
}
// ...
}
Explanations:
I have used the JMSDiExtraBundle's annotations. You need this bundle to make them work.
Otherwise you should declare your controller as a service and inject the EntityManager manually ( for example in your bundle's Resources/config/services.xml ) in the service container.
Declare your controller as Service with #DI\Service annotation.
Inject your EntityManager here to be be able to access it throughout the class with $this->em with the #DI\Inject annotation.
Use FOSRest's #View annotation.
Don't forget to set sensio_framework_extra.view: { annotations: false } before using this if you have the SensioFrameworkExtraBundle's in your application.
Make sure you have return $this; at the end of your User entity's setBirthdate(...) and addClub(...) functions.
Please not that i have used [JMSDiExtraBundle's property injection][3] in the example.
The bundle has to be installed in order to be used.
You might be able to slim down the controller further by using the NoxLogicMultiParamBundle.
I cannot post more than 2 links because im new over here...
please look for the following resources on google:
NoxLogicMultiParamBundle
FOSRestBundle documentation
JMSDiExtraBundle documentation
user:
pattern: /api/user/{id}
defaults: { _controller: AcmeMainBundle:User:putUsers, id: 1 }
or you could use the FQCN:
defaults: { _controller: Acme\MainBundle\Controller\UserController::putUsersAction, id: 1 }
and to match only PUT request use the below code, although some browsers don't support PUT and DELETE see this link
user:
pattern: /api/user/{id}
defaults: { _controller: Acme\MainBundle\Controller\UserController::putUsers, id: 1 }
requirements:
_method: PUT
...and of course to see all of your routes, run this from your project folder:
php app/console router:debug

Resources