简体   繁体   中英

Symfony2 get controller base route name

I'm trying to get the Controller Class base route from within one of my methods. :

    /**
     * @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'     
         }


     }

I've tried:

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

But there is nothing useful in there as far as I can see:

Request Context

Also, you can get a route if you know the name:

Route Collection

But obviously, I don't in this instance.

From the Docs I found it:

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

gives me 'buy-stuff' or 'sell-stuff';

depending on my entry point.

Try this:

$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

There is no such thing as a "base route". The name definitions on these routes will have no effect.

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

You have a Route Prefix configured in your Controller which does not have a name.

Get the current route name in a container-aware service / Controller with:

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

The second option is the magic insertation of $_route in your controller.

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

In Twig get your route like the following ( only works for master requests , not forwarded ones - use carefulyl with ESI )

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

What you are trying to do can be accomplished by including a parameter in your route names and have two routing configurations each having a seperate prefix from a seperate container parameter.

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

Now create two routing configurations:

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"

You can also use a special $_route variable, which is set to the name of the route that was matched.

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'));

        // ...
    }

    // [...]
}

More info : https://symfony.com/doc/current/book/routing.html#route-parameters-and-controller-arguments

You must use the $_route in your action controller.

/**
 * @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,
        );
     }


 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM