Symfony - Route 404 - symfony

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)

Related

I've defined a bundle Controller and Routing, but I get "Error: the controller does neither exist as service nor as class"

I have created a controller class under a module directory in Symfony 4. My bundle's routing file references it. My root's routing file includes the bundle's routing file.
However I get an error 500, "Error: the controller does neither exist as service nor as class".
Why?
root/src/MyBundle/Resources/config/routing.YML
route_name:
path: /test3
controller: MyBundle\Controller\ExportCsvController::exportProductInCsv
options:
expose: true
root/config/routes/my_custom_routes.YML
route_name:
resource: "#MyBundle/Resources/config/routing.yml"
prefix: /
root/src/MyBundle/Controller/ExportCsvController.PHP
<?php
namespace MyBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ExportCsvController extends Controller
{
public function exportProductInCsv(): Response
{
return new Response(
'<html><body>test</body></html>'
);
}
}
It was a problem of cache (Symfony cache). Solved with this command: php bin/console cache:clear --env=prod. Now the content of the file root/src/MyBundle/Resources/config/routing.YML is correctly taken into account :).

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

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"

Can't post to symfony2

I'm using Symfony2 and i'm trying to POST a form to my controller, but my controller take it as a GET request everytime... Even when i'm using restclient, i put POST for the request but when i use $request->getMethod(), the answer is always GET :/
The source code is really simple :
<?php
namespace TC\UserBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class UserController extends Controller
{
/**
* #Route("/user/register")
* #Method({"POST"})
*/
public function registerAction(Request $request)
{
echo $request->getMethod();
return new Response();
}
}
And when i add the requirements _method: POST like this :
tc_user_register:
path: /user/register/
defaults: { _controller: TCUserBundle:User:register }
requirements:
_method: POST
I got this :
No route found for "POST /user/register"
Whatever i do, Symfony2 take it as a GET request, any idea ?
Thanks !
Couple of things #Xtroyer:
1 If you use routing.yml file, requirements: _method is not recognised since Symfony 2.2, it is now:
tc_user_register:
path: /user/register/
defaults: { _controller: TCUserBundle:User:register }
methods: [POST]
2 If you use Annotations. #Method is not recognised if you don't mention the following use statement in your controller:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
3 You can check what route is exactly registered with the following command?
php app/console router:debug | grep tc_user_register
You should have:
Name Method Scheme Host Path
tc_user_register POST ANY ANY /user/register/

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

symfony2 Unable to find controller

I am trying to start on Symfony2 but ran into a problem right away following the Symfony 2 "the book" part "Creating pages in Symfony 2":
I did this:
Created the bundle
php app/console init:bundle "Acme\StudyBundle" src
*Added the namespace in app/autoload.php *
$loader->registerNamespaces(array(
'Acme' => __DIR__.'/../src',
));
Initialized the bundle
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Acme\StudyBundle\AcmeStudyBundle(),
);
// ...
return $bundles;
}
Created the routes in app/config.routing.yml and src/Acme/StudyBundle/Resources/config/routing.yml
# app/config/routing.yml
homepage:
pattern: /
defaults: { _controller: FrameworkBundle:Default:index }
hello:
resource: "#AcmeStudyBundle/Resources/config/routing.yml"
# src/Acme/StudyBundle/Resources/config/routing.yml
hello:
pattern: /hello/{name}
defaults: { _controller: AcmeStudyBundle:Hello:index }
Created the controller
// src/Acme/StudyBundle/Controller/HelloController.php
namespace Acme\StudyBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloController
{
public function indexAction($name)
{
return new Response('<html><body>Hello '.$name.'!</body></html>');
}
}
When I load the page: http://localhost/app_dev.php/hello/Ryan Symfony gives me an exception:
Unable to find controller "AcmeStudyBundle:Hello" - class "Acme\StudyBundle\Controller\HelloController" does not exist.
I got over the code several times but cannot find anything wrong.
just add
<?php
in the beginning of your controller file : src/Acme/StudyBundle/Controller/HelloController.php
it's solved the problem to me.
Afaik there is a discussion going on within the Symfony 2.0 dev guys in what places they should keep the "Bundles" extension.
I've just grabbed the latest version of Symfony via Git and followed your code 1:1.
I got various error messages too but when I changed...
in src/Acme/StudyBundle/Resources/config/routing.yml
defaults: { _controller: AcmeStudyBundle:Hello:index }
to
defaults: { _controller: AcmeStudy:Hello:index }
app/config/routing.xml
resource: "#AcmeStudyBundle/Resources/config/routing.yml"
to
resource: "#AcmeStudy/Resources/config/routing.yml"
...i got a pretty "Hello Ryan" in the browser.
Hope this helps!
You are probably running PR9. Update to PR11(latest), and I would bet this gets resolved. Symfony devs removed the 'Bundle' suffix in PR9, but added it back again shortly there after.
Also, Symfony devs keep an Update log that I find extremely helpful.

Resources