Does anyone know a way to get the template (or its name) that called the Twig extension?
You could pass the referrer into the twig template...
class DefaultController extends Controller
{
public function indexAction(Request $request)
{
return $this->render('DefaultBundle:Default:index.html.twig', array('referrer', $request->getRequestUri()));
}
}
Related
let's assume that I'm creating a simple blog and I have two controllers - BaseController and PagesController.
In BaseController I fetching meta tags from database.
In PagesController I have two functions: index() which renders index.html.twig template and view() which renders view.html.twig.
I want to set meta tags variables from BaseController to be global so in PagesController I do not have to render them, just render variables like content which gets the data from database.
How can I do it? And what is the best solution for this?
Note: I want to do it in BaseController, I do not want to write every variable in twig global variables.
One way to achieve this, is something like this,
<?php
class BaseController {
protected function getBaseParameters() {
/**
Do stuff
Do stuff
Do stuff
**/
return $data;
}
protected function getTwig() {
#retrieve twig instance here
return $this->twig;
}
public function render($template, $args) {
return $this->getTwig()->render($template, array_merge($this->getBaseParameters(), $args));
}
}
class PagesController extends BaseController {
public function view() {
$args = [];
/**
Do stuff
Do stuff
Do stuff
**/
return $this->render('view/my/template.html', $args);
}
}
I have this route
edit_project:
pattern: /edit/{id}
defaults: { _controller: CpjProjectsBundle:Project:edit }
requirements:
id: \d+
and this is the controller:
public function editAction(Request $request)
{
}
inside the controller I'm unable to receive id
$this->query->get('id'); //empty
if I change the method signature, it works:
public function editAction($id)
but I need the Request to handle the form, usualy in this way
$form->handleRequest($request);
any suggestion for a workaround?
many thanks
Your final URL looks like this: http://my.domain.com/edit/123 so there is no query part of URL (...?id=123). You should accept both the Request object and id as arguments of your function:
public function editAction(Request $request, $id)
{
var_dump($request, $id);
}
Using that url if you want access to the id you will pass it as another parameter to your function. But it seems like you may want to get the Project entity associated with that id. You can save a step in your controller and have symfony look it up for you by type hinting the $id variable like so:
<?php
namespace Cpj\ProjectsBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Cpj\ProjectsBundle\Entity\Project;
//Other use statements
class PrjectController extends Controller
{
public function editAction(Request $request, Project $id)
{
var_dump($id);
// This will be and instance of Cpj\ProjectsBundle\Entity\Project
//if you need the actual ID of it you can do the following
$realID = $id->getId()
}
}
Hi Have followed the instructions to enable the SensioFrameworkExtraBundle here: http://symfony.com/doc/2.1/bundles/SensioFrameworkExtraBundle/index.html
Thereafter I create the below controller:
namespace Acme\DemoBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class MyController
{
/**
* #Template
*/
public function indexAction()
{
}
}
If set up a route pointing to the indexAction on this controller and browse to it, I get the following error:
The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?
It looks like the SensioFrameworkExtraBundle is not actually enabled, but I cannot figure out why. I'm looking for advice.
The #Template annotation is working. As the error suggest, you must return something. If return array, it will be sent to template engine. Make sure the template exists.
public function indexAction()
{
...
$somedata = 'fill data';
return array('somedata' => $somedata);
}
I have a slugify method in an Twig Extension which i would like to use in some cases in a controller, f.e with redirects.
Is there an easy way for this?
How could i access functions from Twig Extensions in the controller?
Or do i have to make the slugify method somewere as a helper in order to use it in the code and in twig?
Access function / logic from twig and a controller
I think there are two solutions for this, both should use the Twig_Function_Method class.
1
The first solution gilden already posted, is to encapsulate the logic into a service and make a wrapper for the Twig Extension.
2
Another solution is to use only the Twig Extension. The Twig Extensino is already a service, you have to define it as service with the special <tag name="twig.extension" />.
But it's also a service, which instance you can grab by the service container. And it's also possible to inject other services:
So you have your Twig Extension / Service:
class MyTwigExtension extends \Twig_Extension
{
private $anotherService;
public function __construct(SecurityService $anotherService= null)
{
$this->anotherService = $anotherService;
}
public function foo($param)
{
// do something
$this->anotherService->bar($param);
}
public function getFunctions()
{
// function names in twig => function name in this calss
return array(
'foo' => new \Twig_Function_Method($this, 'foo'),
);
}
/**
* Returns the name of the extension.
*
* #return string The extension name
*/
public function getName()
{
return 'my_extension';
}
}
The services.xml looks like this
<service id="acme.my_extension" class="Acme\CoreBundle\Twig\Extension\MyTwigExtension">
<tag name="twig.extension" />
<argument type="service" id="another.service"></argument>
</service>
To acccess to the service from your controller you only have to use this:
$this->container->get('acme.my_extension')
Notice The only difference to a normal service is, that the twig extension is not lazy loaded (http://symfony.com/doc/current/cookbook/templating/twig_extension.html#register-an-extension-as-a-service)
I would advise creating a general service and injecting it to the Twig extension. The extension would act just as a wrapper to the service.
namespace Acme\Bundle\DemoBundle\...;
class MyService
{
public function myFunc($foo, $bar)
{
// some code...
}
// additional methods...
}
EDIT - as mentioned by Squazic, the first argument must implement Twig_ExtensionInterface. An inelegant solution would be to add methods to MyTwigExtension, that in turn call out respective methods in the service.
namespace Acme\Bundle\DemoBundle\Twig\Extension;
class MyTwigExtension extends \Twig_Extension
{
protected $service;
public function __construct(MyService $service)
{
$this->service = $service;
}
public function getFunctions()
{
return array(
'myTwigFunction' => new \Twig_Function_Method($this, 'myFunc'),
'mySecondFunc' => new \Twig_Function_Method($this, 'mySecondFunc'),
);
}
public function myFunc($foo, $bar)
{
return $this->service->myFunc($foo, $bar);
}
// etc...
}
Or another way is to get it via twig... (this is on Symfony 2.7)
$twigExt = $this->container->get('twig')->getExtension(TwigExtensionClassName::class);
So if your Twig extension class is called 'MyFabulousTwigExt', then you'd call
$twigExt = $this->container->get('twig')->getExtension(MyFabulousTwigExt::class);
This worked for me when the above didn't (our extension wasn't also a service)
I've found this to be the best way of calling the extension directly (tested in Symfony 4.4):
use Twig\Environment;
private Environment $twig;
public function __construct(Environment $twig)
{
$this->twig = $twig;
}
public function foo()
{
$extensionOutput = $this->twig
->getExtension(YourExtension::class)
->yourExtensionFunction(
$this->twig,
$value
);
...
}
Useful if you don't want to (or can't) break the logic out of the Twig extension.
I've got an Entity that I want to associate with the users session.
I created a service so that I could reach this info from where ever.
in the service i save the entities id in an session variable
and in the getEntity() method i get the session variable and with doctrine find the entity and return it.
this way to the template i should be able to call {{ myservice.myentity.myproperty }}
The problem is that myservice is used all over the place, and I don't want to have to get it in every since Action and append it to the view array.
Is there a way to make a service accessible from all views like the session {{ app.session }} ?
The solution
By creating a custom service i can get to that from where ever by using
$this->get('myservice');
this is all done by http://symfony.com/doc/current/book/service_container.html
But I'll give you some demo code.
The Service
This first snippet is the actual service
<?php
namespace MyBundle\AppBundle\Extensions;
use Symfony\Component\HttpFoundation\Session;
use Doctrine\ORM\EntityManager;
use MyBundle\AppBundle\Entity\Patient;
class AppState
{
protected $session;
protected $em;
function __construct(Session $session, EntityManager $em)
{
$this->session = $session;
$this->em = $em;
}
public function getPatient()
{
$id = $this->session->get('patient');
return isset($id) ? $em->getRepository('MyBundleStoreBundle:Patient')->find($id) : null;
}
}
Register it in you config.yml with something like this
services:
appstate:
class: MyBundle\AppBundle\Extensions\AppState
arguments: [#session, #doctrine.orm.entity_manager]
Now we can as I said before, get the service in our controllers with
$this->get('myservice');
But since this is a global service I didn't want to have to do this in every controller and every action
public function myAction()
{
$appstate = $this->get('appstate');
return array(
'appstate' => $appstate
);
}
so now we go create a Twig_Extension
Twig Extension
<?php
namespace MyBundle\AppBundle\Extensions;
use MyBundle\AppBundle\Extensions\AppState;
class AppStateExtension extends \Twig_Extension
{
protected $appState;
function __construct(AppState $appState) {
$this->appState = $appState;
}
public function getGlobals() {
return array(
'appstate' => $this->appState
);
}
public function getName()
{
return 'appstate';
}
}
By using dependency injection we now have the AppState Service that we created in the twig extension named appstate
Now we register that with the symfony (again inside the services section inside the config-file)
twig.extension.appstate:
class: MyBundle\AppBundle\Extensions\AppStateExtension
arguments: [#appstate]
tags:
- { name: twig.extension }
The important part being the "tags", since this is what symfony uses to find all twig extensions
We are now set to use our appstate in our twig templates by the variable name
{{ appstate.patient }}
or
{{ appstate.getPatient() }}
Awesome!
Maybe you can try this in your action ? : $this->container->get('templating')->addGlobal($name, $value)