doukun8944
2016-03-17 18:54 阅读 33
已采纳

如何使用routing.yml匹配所有控制器和操作

I am working on a project where I have about 98 controllers. We have decided to migrate this project to Symfony. I have been able to create a custom Bundle called "mdBundle".

It gets registered and I able to access the controller without any issue if I use type: annotation in the routing.yml file.

However because we have so many controllers it will take forever to create the anotations in the actions. Therefore I am trying to come up with a way that my routing.yml can handle any request and execute the controller requested.

Right now my routing.yml looks like this:

mdRoute:
    path: /{_controller}/{_action}/
    defaults: { _controller: mdBundle:_controller:_action }

My Controller(DefaultController.php) is like this:

namespace mdBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class DefaultController extends Controller
{

public function indexAction(Request $request)
{
    return new Response('foo');

}

public function testingAction(Request $request)
{
    return new Response('Bar');

}
}

When I run it I get the following error: LogicException in ControllerResolver.php line 69: Unable to parse the controller name "Default".

My folder structure is a bit different than symfony's default. My Bundle is inside /root/app/mdBundle/ (<-- From here it is the same as Symfony). Anybody has any idea why this is not working.

Or what could be another way to accomplish this without having to annotate every single action in my 98 contollers?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    doujiu8826 doujiu8826 2016-03-17 19:33

    You can import all routes for a specific controller, or for a whole Controller directory.

    This will import all routes defined inside all your controllers from MdBundle.

    #app/config/routing.yml
    app_mdbundle:
        resource: "@MdBundle/Controller"
    

    This will import all routes defined inside a specific controller inside MdBundle:

    #app/config/routing.yml
    app_md_bundle:
        resource: "@MdBundle/Controller/YourController"
        type:     annotation
    

    Take a look here: http://symfony.com/doc/current/book/routing.html

    You really should define your bundle inside src/ directory. And for this you have two options: let the robots do the job for you ( using: php app/console generate:bundle, when you are inside the root directory [ and if you're using Symfony < v3 ], or $ php bin/console generate:bundle if you're using Symfony >= 3] ), or by creating it manually: inside src/, create new dirs (MdBundle/Controller/YourController1.php, and inside MdBundle you can create the other dirs, like Command,Resources/views, Resources/config, etc). Take a look here: http://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_bundle.html

    And don't use _controller as parameter for that route. http://symfony.com/doc/current/book/controller.html The rule is simple:

    mdRoute:
        path: /hello/{name}
        defaults: { _controller: mdBundle:Default:yourAction }
    

    This will match all routes like /hello/*, and it will execute the yourAction action method inside DefaultController inside mdBundle.

    LE:

    Yes, you can accomplish that too:

    # src/MdBundle/Controller/HelloController.php
    class HelloController{
        /**
          * @Route("/hello/foo", name="whatever1")
          */
        public function fooAction(){ ... }
    }
    
    # src/MdBundle/Controller/Hello2Controller.php
    class Hello2Controller{
        /**
          * @Route("/hello2/foo", name="whatever2")
          */
        public function fooAction(){ ... }
    }
    

    And if you don't want to write all the routes above the action methods, you can create MdBundle/Resources/config/routing.yml, and here put all your routes:

    app_hello:
        path: /hello/foo
        defaults: { _controller: MdBundle:Hello:foo }
    
    app_hello2:
        path: /hello2/foo
        defaults: { _controller: MdBundle:Hello2:foo }
    

    And then import all routes in the main config file:

    # app/config/routing.yml
    MdBundle:
        resource: "@MdBundle/Resources/config/routing.yml"
    

    Or define, directly, the routes above, in the main app/config/routing.yml file.

    点赞 评论 复制链接分享

相关推荐