Symfony2 Route annotation on class not working - php

So basically I have a controller of a bundle in which i want to and a route prefix so i use the #Route annotation on the class, i've done this all of the other controllers of my Symfony2 app. However this one does not take the prefix into account so instead of being able to access the page on /admin/users/list i only access it on /list.
Here is the controller :
<?php
namespace LanPartyOrg\UserBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use JMS\SecurityExtraBundle\Annotation\PreAuthorize;
/*
* #Route("/admin")
*
*/
class AdminController extends Controller
{
/**
* #Route("/list", name="users_list")
* #Template("LanPartyOrgUserBundle:Admin:List.html.twig")
*/
public function listAction(){
$em = $this->getDoctrine()->getManager();
$users = $em->getRepository('LanPartyOrgUserBundle:User')->findAll();
return array('users'=>$users);
}
}
And here is my routing.yml :
lan_party_org_user:
resource: "#LanPartyOrgUserBundle/Controller/"
type: annotation
prefix: /
fos_user_security:
resource: "#FOSUserBundle/Resources/config/routing/security.xml"
fos_user_profile:
resource: "#FOSUserBundle/Resources/config/routing/profile.xml"
prefix: /profile
fos_user_register:
resource: "#FOSUserBundle/Resources/config/routing/registration.xml"
prefix: /register
fos_user_resetting:
resource: "#FOSUserBundle/Resources/config/routing/resetting.xml"
prefix: /resetting
fos_user_change_password:
resource: "#FOSUserBundle/Resources/config/routing/change_password.xml"
prefix: /profile
Thanks for any help

Annotations must be added to docblocks, not just simple comments.
You need to start your comment with /** instead of /* (notice double *):
/**
* #Route("/admin")
*/
class AdminController extends Controller
{
// ...
}
This is going to prefix all your AdminController's routes with /admin.

Your following code
/*
* #Route("/admin")
*
*/
should be as
/**
* #Route("/admin")
*
*/
and also make sure that two controller should not have same prefix .

Related

Symfony4. ParamConverter annotation conflicts injecting a service by autowire

When I try to use #ParamConverter annotation in a Controller action, I get an error
"Cannot resolve argument $company of \"App\\Controller\\ProfileController::top()\": Cannot autowire service \".service_locator.0CrkHeS\": it references class \"App\\Document\\Company\" but no such service exists."
I know that such a service does not exist, because I've excluded Document path in services.yaml. I just need to find a Company document object from Repostiroy.
Here is my controller code:
<?php
// src/Controller/ProfileController.php
namespace App\Controller;
use App\Document\Company;
use App\Service\DocumentManager\CompanyManager;
use FOS\RestBundle\Controller\FOSRestController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Swagger\Annotations as SWG;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* #Route("/profile")
*/
class ProfileController extends FOSRestController
{
/**
* #Route("/top/{id}")
* #Method("GET")
* #SWG\Response(
* response=200,
* description="Returns top profiles",
* )
* #SWG\Tag(name="profile")
*
* #ParamConverter("company", class="App\Document\Company")
* #param CompanyManager $companyManager
* #return Response
*/
public function top(CompanyManager $companyManager, Company $company)
{
dump($company->getId());exit;
return $this->handleView($this->view($companyManager->getTopProfiles(), Response::HTTP_OK));
}
}
services.yaml configuration:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
App\:
resource: '../src/*'
exclude: '../src/{Entity,Document,Migrations,Tests,Kernel.php,Exception,DataFixtures}'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
In case somebody else will get same problem.
The problem had nothing to do with autowire conflict. #ParamConverter didn't work, because I used mongoDB and Doctrine ODM, not ORM. By default Doctrine ParamConverter won't work for mongo documents.
So I found some information here
https://matthiasnoback.nl/2012/10/symfony2-mongodb-odm-adding-the-missing-paramconverter/
Define a new service in services.yaml file:
doctrine_mongo_db_param_converter:
class: Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\DoctrineParamConverter
tags:
- { name: request.param_converter, converter: doctrine.odm }
arguments: ['#doctrine_mongodb']
Then #ParamConverter should work OK now.

Symfony2 new bundle routing

I have create new bundle in src\Moda\CategoryBundle\Controller\DefaultController.php
and a change routing to:
namespace Moda\CategoryBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class DefaultController extends Controller
{
/**
* #Route("/show", name="_show")
* #Template()
*/
public function indexAction()
{
die('test');
return array();
}
}
and my routing.yml in app/config
moda_category:
resource: "#ModaCategoryBundle/Controller/"
type: annotation
prefix: /
This links dosnt work:
localhost/web/app_dev.php/category/show
localhost/web/app_dev.php/show
Do you know what I am doing wrong?
I think you should import the config.yml file inside your bundle.
So instead of :
moda_category:
resource: "#ModaCategoryBundle/Controller/"
type: annotation
prefix: /
Change it to:
moda_category:
resource: "#ModaCategoryBundle/Resources/config/routing.yml"
type: annotation
prefix: /
And then add the routes you need inside that file.

Why is my Symfony route is not working?

I made a new Symfony2 bundle and deleted the Acme bundle.
Then I created a new Controller (MainController.php):
<?php
namespace My\BlogBundle\Controller;
class MainController extends Controller
{
/**
* #Route("/", name="index")
* #Template()
*/
public function indexAction()
{
return array();
}
And a simple view: (Main/index.html.twig) which only contains a hello. My routing.yml is empty. When I run the whole project I get:
No route found for "GET /"
404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException ยป
What is wrong here and how to solve it?
Here is my routing debug:
\Symfony>php app/console router:debug
[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
I also cleared the cache with no success.
Here is the routes.yml:
my_blog:
resource: "#MyBlogBundle/Resources/config/routing.yml"
prefix: /
and the routing.yml in MyBlogBundle/Resources/config/routing.yml is empty.
The way your routes.yml is setup, you are requesting the routing.yml file from your bundle.
If you want to use annotations to manage the routes in your bundle, you have to write the routes.yml the following way:
my_blog:
resource: "#MyBlogBundle/Controller/MainController.php"
prefix: /
type: annotation
And your controller needs to include the Route class from the FrameworkExtraBundle:
<?php
namespace My\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class MainController extends Controller
{
/**
* #Route("/", name="index")
* #Template()
*/
public function indexAction()
{
return array();
}
}
This assumes you have installed the SensioFrameworkExtraBundle (http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html#installation).
More information on the route annotation: http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html
For others troubleshooting the situation where the router annotations is correctly installed, but the routes aren't working, you may need to install
composer require symfony/apache-pack
You also could declare your routing for your annotation with:
blog_index:
path: /
defaults: {_controller:BlogBundle:index}
in your BlogBundle/config/routing.yml
and in root, set
blog:
resource: "#BlogBundle/Resources/config/routing.yml"
prefix: /
then in your MainController, the annotation should work:
..use Symfony\Component\Routing\Annotation\Route;
/**
* #Route("/", name="blogindex")
*/
Add following annotation for the MainController class:
/**
* #Route("/")
*/
class MainController extends Controller
{
}
you'r problem is that you have referenced the routing as yml not annotations, if you want to use annotations you must declare at your front route att the app folder as
post:
resource: "#AcmeBlogBundle/Controller/PostController.php"
type: annotation
and at the PostController class you define
use Symfony\Component\Routing\Annotation\Route;
/**
* #Route("/")
*/
class MainController extends Controller
{
}
and the function
/**
* #Route("/add", name="article_add")
*/
public function add(){
...
}
See the referance at the docs

Symfony 2 - No route found for "GET /videotheque"

I need some help because I didn't succeed in finding the source of my route problem.
Here is my routing.yml:
gstyle39VideothequeBundle:
resource: "#gstyle39VideothequeBundle/Resources/config/routing.yml"
prefix: /videotheque
The routing.yml in VideothequeBundle/Resources/config/:
VideothequeBundle_homepage:
pattern: /
defaults: { _controller: gstyle39VideothequeBundle:Videotheque:index }
My controller:
<?php
namespace gstyle39\VideothequeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Response;
class VideothequeController extends Controller
{
public function indexAction()
{
return new Response("Kikoo");
}
}
For my part I did a "cache:clear", a "router:debug" which detected my route:
VideothequeBundle_homepage ANY /videotheque/
I also manually deleted the folder app/cache...
IMHO you didn't add routing definition in application global routing.yml (app/config/routing.yml).
Look at my routing definitions in https://github.com/drupalmk/Jobeeto

How can I add some route prefix to all controllers in Symfony2?

I am using annotations to define routes in controllers and I have 15 controllers. All are executed by /path1 , /path2.
Is there any way that in all those controller , I can access them via /admin/path1 and /admin/path2?
I don't want to enter that by changing each file.
Can I do that from a single location? I mean the whole bundle should open via /admin and then their respective paths.
try this
# app/config/routing.yml
acme_hello:
resource: "#AcmeHelloBundle/Resources/config/routing.yml"
prefix: /admin
or if using annotations
resource: "#AcmeHelloBundle/Controller"
type: annotation
prefix: /admin
Use this in routing.yml:
Admin:
resource: "#AdminBundle/Controller"
type: annotation
prefix: /admin
Just define the annotation for your Class (not for method)
/**
* #Route("/blog")
*/
http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html#route-prefix
If you want to prefix specific controller DevController for example and have something like:
myproject.com/dev/test
in your Controller add the following Route annotation as in example:
/**
* #Route("/dev")
*/
class DevController extends Controller{
/**
* #Route("/test")
*/
public function testSavingAction(){
return new Response();
}
....

Categories