Багато місяців тому я мав таку ж проблему, як і ви, і "гугливши"
я знайшов робочий код і пристосував його до своїх потреб. Ось і
ми:
1 - We need to define a TWIG extension for
that. We create the folder structure
Your\OwnBundle\Twig\Extension if you haven't defined
yet.
2 - Inside this folder we create the file
ControllerActionExtension.php which code is:
namespace Your\OwnBundle\Twig\Extension;
use Symfony\Component\HttpFoundation\Request;
/**
* A TWIG Extension which allows to show Controller and Action name in a TWIG view.
*
* The Controller/Action name will be shown in lowercase. For example: 'default' or 'index'
*
*/
class ControllerActionExtension extends \Twig_Extension
{
/**
* @var Request
*/
protected $request;
/**
* @var \Twig_Environment
*/
protected $environment;
public function setRequest(Request $request = null)
{
$this->request = $request;
}
public function initRuntime(\Twig_Environment $environment)
{
$this->environment = $environment;
}
public function getFunctions()
{
return array(
'get_controller_name' => new \Twig_Function_Method($this, 'getControllerName'),
'get_action_name' => new \Twig_Function_Method($this, 'getActionName'),
);
}
/**
* Get current controller name
*/
public function getControllerName()
{
if(null !== $this->request)
{
$pattern = "#Controller\\\([a-zA-Z]*)Controller#";
$matches = array();
preg_match($pattern, $this->request->get('_controller'), $matches);
return strtolower($matches[1]);
}
}
/**
* Get current action name
*/
public function getActionName()
{
if(null !== $this->request)
{
$pattern = "#::([a-zA-Z]*)Action#";
$matches = array();
preg_match($pattern, $this->request->get('_controller'), $matches);
return $matches[1];
}
}
public function getName()
{
return 'your_own_controller_action_twig_extension';
}
}
3 - After that we need to specify the service
for TWIG to be recognized:
services:
your.own.twig.controller_action_extension:
class: Your\OwnBundle\Twig\Extension\ControllerActionExtension
calls:
- [setRequest, ["@?request="]]
tags:
- { name: twig.extension }
4 - Cache clear to make sure everything is
ok:
php app/console cache:clear --no-warmup
5 - And now, if I'm not forgetting anything,
you will be able to access those 2 methods in a TWIG template:
get_controller_name()
and
get_action_name()
6 - Examples:
You are in the {{ get_action_name() }} action of the {{ get_controller_name() }} controller.
This will output something like: You are in the index action
of the default controller.
Також можна скористатися для перевірки:
{% if get_controller_name() == 'default' %}
Whatever
{% else %}
Blablabla
{% endif %}
І це все !! Сподіваюся, що допоміг вам, товариш :)
Edit: Take care about the clearing cache. If
you don't use --no-warmup
parameter maybe you will
realize that nothing is shown in your templates. That's because
this TWIG Extension uses the Request to extract the Controller and
Action names. If you "warm up" the cache, the Request is not the
same as a browser request, and the methods can return
''
or null