【问题标题】:Catch-all route in Symfony 3Symfony 3 中的包罗万象的路线
【发布时间】:2016-05-24 01:51:30
【问题描述】:

我在 Symfony2 中有一条包罗万象的后备路线,但我无法在 Symfony3 中工作。我尝试了这种确切的语法(我的 Symfony2 路由的逐字副本),但没有奏效。

fallback:
    path:     /{req}
    defaults: { _controller: MyBundle:Default:catchAll }
    requirements:
        req: ".+"

我怎样才能让它在 Symfony3 中工作? (它实际上是唯一阻碍我使用 Symfony3 并使我保持在 v2.8 的事情)

【问题讨论】:

    标签: php routes yaml symfony


    【解决方案1】:

    这应该对你有帮助:

    route1:
      path: /{req}
      defaults: { _controller: 'AppBundle:Default:index' }
      requirements:
          req: ".+"
    

    其中,我的控制器被称为“DefaultController”,而我有一个名为“indexAction()”的函数。

    这是我的 DefaultController 代码:

    class DefaultController extends Controller
    {
        /**
         * @Route("/", name="homepage")
         */
        public function indexAction(Request $request)
    ...
    

    我确实在我的环境中尝试过你所说的,直到我指定了正确的控制器设置,它才起作用。


    编辑:

    为此,必须将参数Request $request带有类型提示)添加到操作的方法签名中。

    【讨论】:

    • @TylerSebastian,我更新了我的帖子以显示我的 DefaultController 代码。顺便说一句,我确实测试了 ops 代码以验证它不像他说的那样工作,然后进行了所示的更改,它确实提供了一条全面的路线。我只是通过在我的 URL 中添加“/home”来验证。
    • @AlvinBunk 很有趣,您为什么认为需要 Request $request 参数?几乎没有关于这方面的文档,因此我首先提出了问题。谢谢,这似乎有效!
    • 你好@bought777。你不需要那个。我只是剪切并粘贴了很多代码。你可以使用这个:public function indexAction()
    • 我在没有 $request 作为参数的情况下尝试了它,但它没有用。 :O
    • 我必须查看您拥有的其余控制器代码才能复制正在发生的事情。不管怎样,我想你已经准备好了。享受吧!
    【解决方案2】:

    我发现当前接受的答案几乎Symfony 4 很有用,所以我要添加我的解决方案:


    这是我为使其在 Symfony 4 中工作所做的工作:

    • 打开/src/Controller/DefaultController.php,确保有一个名为index(){}的函数
      • 按照某些评论的建议,不需要Request $request 添加为第一个参数。
      • 这是处理routes.yaml捕获的所有个url的方法
    • 打开/config/routes.yaml,添加这个:

      yourRouteNameHere:
        path: /{req}
        defaults: { _controller: 'App\Controller\DefaultController::index' }
        requirements:        #  the controller --^     the method --^
          req: ".*"`  # not ".+"
      

    【讨论】:

    • 为什么需要索引?只需让您的控制器可调用。
    • 因为这种方式对初学者来说更容易,如果你需要,也更容易扩展。一个 /admin 也是如此。
    • @emix 魔术方法?不,谢谢。我非常谨慎地使用它们,这就是我给大多数初学者的建议:谨慎使用它们。
    • 我说的不是__set__get__invoke 方法非常方便,特别是对于控制器和 CQ 处理程序。这样的对象可以用callable 类型提示。
    【解决方案3】:

    您也可以override Exception controller

    # app/config/config.yml
    twig:
        exception_controller:  app.exception_controller:showAction
    
    # app/config/services.yml
    services:
        app.exception_controller:
            class: AppBundle\Controller\ExceptionController
            arguments: ['@twig', '%kernel.debug%']
    
    namespace AppBundle\Controller;
    
    use Symfony\Component\Debug\Exception\FlattenException;
    use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;
    
    class ExceptionController
    {
        protected $twig;
    
        protected $debug;
    
        public function __construct(\Twig_Environment $twig, $debug)
        {
            $this->twig = $twig;
            $this->debug = $debug;
        }
    
        public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null)
        {
            // some action
    
            return new Response($this->twig->render('error/template.html.twig', [
                    'status_code' => $exception->getStatusCode()
                ]
            ));
        }
    }
    

    【讨论】:

    • 谢谢,一个很好的帮助,但这不适用于异常处理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多