【问题标题】:Symfony2 get controller base route nameSymfony2 获取控制器基本路由名称
【发布时间】:2013-05-19 22:29:10
【问题描述】:

我正在尝试从我的一种方法中获取控制器类基本路由。 :

    /**
     * @Route("/buy-stuff", name="buy-stuff")
     * @Route("/sell-stuff" , name="for-sale")
     */
    class SalesController extends Controller
    {
        /**
         * @Route("/", name="salesindex")
         * @Template()
         */
         public function indexAction()
         {  
            //Get the entry route here. eg: 'buy-stuff' or 'sell-stuff'     
         }


     }

我试过了:

$this->container->get('router')->getContext()

但据我所知,那里没有任何用处:

Request Context

另外,如果你知道名字,你可以得到一条路线:

Route Collection

但显然,在这种情况下我不会。

【问题讨论】:

  • 请查看我的回答并询问是否有任何不清楚的地方,以便我更新。否则请接受答案:)
  • 我解决了:从我找到的文档中: $this->container->get('request')->getPathInfo();根据我的入口点给我“买东西”或“卖东西”api.symfony.com/2.0/Symfony/Component/HttpFoundation/…
  • 这只是故事的一半...如果您将其中一个前缀更改为 /foo getPathInfo() 将返回 foo 而不是您与之关联的名称(即 buy-stuff )是你最初的问题:)
  • 我想要路线。因此,如果我将基本路由更改为“foo”,我希望在 indexAction() 中使用“foo”

标签: php symfony


【解决方案1】:

Docs我找到了:

$this->container->get('request')->getPathInfo();

给我“买东西”或“卖东西”;

取决于我的入口点。

【讨论】:

    【解决方案2】:

    试试这个:

    $name = 'buy-stuff'; // controller action name
    /** @var RouterInterface $router */
    $router = $this->container->get('router');
    $route = $router->getRouteCollection()->get($name);
    $controllerAction = $route->getDefault('_controller');
    // also you can get a lot of information from the $route variable
    

    【讨论】:

      【解决方案3】:

      没有所谓的“基本路线”。这些路由上的名称定义将不起作用。

       /**
        * @Route("/buy-stuff", name="buy-stuff")
        * @Route("/sell-stuff" , name="for-sale")
        */
       Class XY 
       {
           // ...
       }
      

      您在控制器中配置了一个没有名称的 Route Prefix

      在容器感知服务/控制器中获取当前路由名称:

      $route = $this->container->get('request')->get('_route');
      

      第二个选项是在控制器中神奇地插入 $_route。

      class MyController extends Controller
      {
          public function myAction($_route)
          {
              // ...
      

      在 Twig 中获取您的路由,如下所示(仅适用于主请求,不适用于转发的请求 - 将 careyl 与 ESI 一起使用)

      {{ app.request.attributes.get('_route') }}
      

      您可以通过在路由名称中包含一个参数来完成您想要做的事情,并有两个路由配置,每个配置都有一个来自单独容器参数的单独前缀。

      # app/config/config.yml
      parameters:
          acme.routep_refix.buy_stuff: /buy-stuff
          acme.route_prefix.for_sale:  /for-sale
      

      现在创建两个路由配置:

      acme.buy_stuff:
          prefix:    %acme.route_prefix.buy_stuff%
          resource: "@AcmeHelloBundle/Resources/config/routing_buy_stuff.yml"
      
      acme.buy_stuff:
          prefix:    %acme.route_prefix.for_salef%
          resource: "@AcmeHelloBundle/Resources/config/routing_for_sale.yml"
      

      【讨论】:

      • $route = $this->container->get('request')->get('_route');给我 'salesindex',$_route 也是。
      • 这是正确的行为。没有命名路由前缀之类的东西。必须用参数解决。给我几分钟,我会举个例子:)
      • 我需要的是:'$this->container->get('request')->getPathInfo();'
      • 这可能对您有用,可以作为解决方案实施,但这不是您问题的答案。 getPathInfo 返回路径(即 /for-sale )而不是您问题中要求的路线名称:)
      • 感谢您的帮助。我认为该解决方案确实回答了我的问题。我没有要求基本路线名称,只是基本路线。
      【解决方案4】:

      您还可以使用特殊的$_route 变量,将其设置为匹配的路由名称。

      class BaseController extends Controller
      {
          // ...
      
          /**
           * @param $_route
           */
          public function testAction($_route)
          {
              dump($_route);
      
              // Or by request_stack:
              dump($this->get('request_stack')->getCurrentRequest()->attributes->get('_route'));
      
              // ...
          }
      
          // [...]
      }
      

      更多信息:https://symfony.com/doc/current/book/routing.html#route-parameters-and-controller-arguments

      【讨论】:

        【解决方案5】:

        您必须在动作控制器中使用$_route

        /**
         * @Route("/stuff", name="stuff")
         */
        class SalesController extends Controller
        {
            /**
             * @Route("/buy", name="stuff_buy")
             * @Route("/sell" , name="stuff_sale")
             * @Template()
             */
             public function indexAction($_route)
             {  
                if ($_route === 'stuff_buy') {
                    $something = '...';
                }
                if ($_route === 'stuff_sale') {
                    $something = '...';
                }
        
                return array(
                    'something' => $something,
                );
             }
        
        
         }
        

        【讨论】:

          猜你喜欢
          • 2015-10-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-24
          • 2019-07-23
          相关资源
          最近更新 更多