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 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳