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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化