doukun8944 2016-03-17 18:54
浏览 46
已采纳

如何使用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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题