Reputation: 179
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:
Also, you can get a route if you know the name:
But obviously, I don't in this instance.
Upvotes: 0
Views: 15894
Reputation: 2298
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
Upvotes: 1
Reputation: 9913
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,
);
}
}
Upvotes: 0
Reputation: 18846
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
Upvotes: 3
Reputation: 179
From the Docs I found it:
$this->container->get('request')->getPathInfo();
gives me 'buy-stuff' or 'sell-stuff';
depending on my entry point.
Upvotes: 3
Reputation: 52493
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"
Upvotes: 2