Symfony Twig global variables - symfony

I'm trying to call some data in my root twig template and used Twig global variables to achieve that.
When I call the service i get this error:
Type error: Argument 1 passed to AwarenessBundle\Service\AwarenessService::getLatestNews() must implement interface Doctrine\ORM\EntityManagerInterface, none given, called in /var/www/html/myisms/vendor/twig/twig/lib/Twig/Template.php on line 675
Twig code:
{{ news_service.getlatestNews() }}
services.yml:
news.latest:
class: AwarenessBundle\Service\AwarenessService
arguments: [ '#doctrine.orm.entity_manager' ]
AwarenessService
class AwarenessService
{
public function getLatestNews(EntityManagerInterface $em)
{
return $em->getRepository("AwarenessBundle:Feed")
->findBy(array(), array('id' => 'DESC', 10));
}
}
I'm not sure where my problem is but I have to make my service global and I don't know how to do that. Any help would be appreciated.

Pass EntityManagerInterface to your service constructor(instead of getLatestNews method), like this:
class AwarenessService
{
/**
* #var EntityManagerInterface
*/
protected $em;
public function __construct(EntityManagerInterface $em){
$this->em = $em;
}
public function getLatestNews() {
return $this->em->getRepository("AwarenessBundle:Feed")
->findBy(array(), array('id' => 'DESC', 10));
}
}
Service.yml:
news.latest:
class: AwarenessBundle\Service\AwarenessService
//arguments for constructor!
arguments: [ '#doctrine.orm.entity_manager' ]

Related

Using Doctrine in a service Symfony2

How do I use Doctrine in a class outside from the controller?
$event = $this->getDoctrine()
->getRepository('AtotrukisMainBundle:Event')
->findByCreatedBy($userId);
if (!$event) {
throw $this->createNotFoundException(
'You have no events'
);
}
The code above works perfectly in a controller, but in a service I get error: Attempted to call method "getDoctrine" on class "Atotrukis\MainBundle\Service\EventService" in /var/www/src/Atotrukis/MainBundle/Service/EventService.php line 15.
How do I make it work?
services.yml:
services:
eventService:
class: Atotrukis\MainBundle\Service\EventService
Part from the EventController:
public function readMyEventsAction()
{
$user = $this->getDoctrine()->getRepository('AtotrukisMainBundle:User')
->findOneById($this->get('security.context')->getToken()->getUser()->getId());
$userEvents = $this->get('eventService')->readUserEvents($user);
return $this->render('AtotrukisMainBundle:Event:myEvents.html.twig', array('events' => $userEvents));
}
EventService.php:
<?php
namespace Atotrukis\MainBundle\Service;
class EventService{
public function create(){
}
public function readUserEvents($userId){
$event = $this->getDoctrine()
->getRepository('AtotrukisMainBundle:Event')
->findByCreatedBy($userId);
if (!$event) {
throw $this->createNotFoundException(
'You have no events'
);
}
return $userId;
}
}
You can pass it as an argument in your service declaration:
services:
eventService:
class: Atotrukis\MainBundle\Service\EventService
arguments: ["#doctrine.orm.entity_manager"]
Then just add a constructor to your class:
protected $em;
public function __construct($em)
{
$this->em = $em
}

Symfony2 - Twig extension does not exist in Twig file

I am trying to register (read the docs) a Twig extension and everything seems to be correct except it's not being found in the Twig file.
Getting the following error:
The function "getPostCount" does not exist in AcmeDemoBundle:Page:index.html.twig at line 17
Can someone show me what I am doing wrong?
services.yml
acme.twig.acme_extension:
class: Acme\DemoBundle\Twig\PostExtension
tags:
- { name: twig. extension }
arguments:
em: "#doctrine.orm.entity_manager"
PostExtension.php
class PostExtension extends \Twig_Extension
{
private $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function getFilters()
{
return array(
);
}
public function getFunctions()
{
return array(
'getPostCount' => new \Twig_Function_Method($this,'getPostCount')
);
}
public function getPostCount($year, $month)
{
return $this->$em->getRepository('AcmeDemoBundle:Post')
->getPostCountsByMonth($year, $month);
}
public function getName()
{
return 'post_extension';
}
}
Twig
{{ getPostCount('2014', 'July') }}
In services.yml:
Remove the extra space in twig.extension.
tags:
- { name: twig.extension }

Symfony2: check-email after registration

First time I'm posting a message in this forum, which I use regularly. I use FOSUserbundle in my Symfony2 application to manage users. I activated the sending of the email confirmation when users create an account by the following thing:
fos_user:
registration:
confirmation:
enabled: true
It works very well: the email is sent successfully. I am redirected to the page /check-email that says that this email has been sent. However, I would like to change the redirection : I would like to be redirected to my index page and not to /check-email. So I did my research and I knew he had to go through the FOSUserBundle events (list here).
What I did :
class RegistrationListener implements EventSubscriberInterface {
private $router;
public function __construct(UrlGeneratorInterface $router) {
$this->router = $router;
}
public static function getSubscribedEvents() {
return array(
FOSUserEvents::REGISTRATION_CONFIRM => 'onRegistrationConfirm');
}
public function onRegistrationConfirm(FormEvent $event) {
$url = $this->router->generate('listeArticlesAccueil');
$event->setResponse(new RedirectResponse($url));
}
}
and services.yml
services:
listener_user.registration_listener:
class: My\Soft\UserBundle\EventListener\RegistrationListener
arguments: [#router]
tags:
- { name: kernel.event_subscriber }
The problem is that every time I am redirected to the page /check-email. I therefore told me that it was probably the wrong event. So I also tried REGISTRATION_SUCCESS. But nothing changes. So either I have not used the right event, or I'm doing something wrong.
In either case, I hope you can help me !
Thanks a lot and sorry for my bad english ;)
I know that this question has been posted a long time ago, but i found a solution and I would like to share it to others that have the same issue.
In this case, the event that you should use is REGISTRATION_SUCCESS.
You have to prioritize the listener REGISTRATION_SUCCESS to be called before FOS\UserBundle\EventListener\EmailConfirmationListener::onRegistrationSuccess like this:
public static function getSubscribedEvents()
{
return [
FOSUserEvents::REGISTRATION_SUCCESS => [
['onRegistrationSuccess', -10],
],
];
}
If you don't do it, EmailConfirmationListener will be called earlier and you will be redirected to fos_user_registration_check_email route.
Here is what I did:
class RedirectAfterRegistrationSubscriber implements EventSubscriberInterface
{
private $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function onRegistrationSuccess(FormEvent $event)
{
$event->stopPropagation();
$url = $this->router->generate('homepage');
$response = new RedirectResponse($url);
$event->setResponse($response);
}
public static function getSubscribedEvents()
{
return [
FOSUserEvents::REGISTRATION_SUCCESS => ['onRegistrationSuccess',-10],
];
}
}
And in app/services.yml:
app.redirect_after_registration_subscriber:
class: AppBundle\EventListener\RedirectAfterRegistrationSubscriber
arguments: ['#router']
tags:
- { name: kernel.event_subscriber }
I hope this helps.
I would bet on REGISTRATION_SUCCESS.
The doc (https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/FOSUserEvents.php) says
that this event allows to set the response.
REGISTRATION_CONFIRM only allows to access the user
Instead of that use REGISTRATION_CONFIRM.
Here is the complete code for the event listener:
/**
* Description of RegisterationConfirmListener
*
* #author imran
*/
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\GetResponseUserEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Description of RegisterationConfirmListener
*
* #author imran
*/
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\GetResponseUserEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Listener responsible to change the redirection at the end of the password resetting
*/
class RegisterationConfirmListener implements EventSubscriberInterface {
private $router;
public function __construct(UrlGeneratorInterface $router) {
$this->router = $router;
}
public static function getSubscribedEvents() {
return [
FOSUserEvents::REGISTRATION_CONFIRM => 'onRegisterationConfirm',
];
}
public function onRegisterationConfirm(GetResponseUserEvent $event) {
$url = $this->router->generate('home');
$event->setResponse(new RedirectResponse($url));
}
}
For Services.yml:
app_user.registeration_confirmed:
class: Wishlocker\AppBundle\EventListener\RegisterationConfirmListener
arguments: [ #router ]
tags:
- { name: kernel.event_subscriber }

Doctrine connection in Twig extension - Symfony

According to what I've read online, it should be possible to create Doctrine connection inside Twig extensions file. I'd like to create an extension with filter that would recieve id of category and then return position of that category in the categery tree as it was established in database.
Twig extension file:
(Symfony/src/MyProject/AdminBundle/Twig/MyExtension.php)
<?php
namespace MyProject\AdminBundle\Twig;
class ToolboxExtension extends \Twig_Extension
{
protected $em;
public function __construct($em)
{
$this->em = $em;
}
public function getFilters()
{
return array(
new \Twig_SimpleFilter('path', array($this, 'categoryPath'))
);
}
public function categoryPath($category_id) {
$repository = $this->$em->getRepository('MyProjectAdminBundle:Category');
$category = $repository->findOneById($category_id);
$path = $repository->getPath($category);
return implode(">", $path);
return $category;
}
public function getName()
{
return 'toolbox_extension';
}
}
Services configuration file:
(Symfony/src/MyProject/AdminBundle/Resources/config/services.yml)
services:
myproject.twig.toolbox_extension:
class: MyProject\AdminBundle\Twig\MyExtension
tags:
- { name: twig.extension }
arguments:
em: "#doctrine.orm.entity_manager"
But whenever I use this filter categoryPath, Twig crashes. So the template is loaded only until the first usage of this extension.
For me, solution below work perfect. I found post on google groups which resolve my problem with doctrine in Twig extensions.
In my AppBundle\Resources\services.yml:
app.twig.get_province.extension:
class: AppBundle\Twig\GetProvinceExtension
tags:
- { name: twig.extension }
arguments: [ '#doctrine.orm.entity_manager' ]
In AppBundle\Twig\GetProvinceExtention.php:
class GetProvinceExtension extends \Twig_Extension {
/**
* #var EntityManager
*/
protected $em;
/**
* GetProvinceExtension constructor.
* #param EntityManager $em
*/
public function __construct(EntityManager $em) {
$this->em = $em;
}
public function getFilters() {
return [
new \Twig_SimpleFilter('province', [$this, 'provinceFilter']),
];
}
public function provinceFilter($code) {
$province = $this->em->getRepository("AppBundle:Province")
->findOneBy([ "code" => $code ]);
return $province->getName();
}
}
Try replacing $this->em-> with $this->$em->.
Try replacing
$this->em->
with $this->$em->
and replacing
$this->em = $em;
with
$this->em = $em['em'];
and
arguments:
em: "#doctrine.orm.entity_manager"
with
arguments: [em: "#doctrine.orm.entity_manager"]
You can use
{% set Products = repository('Entity\\Products').findAll() %}
inside your twig
Then you can do a foreach by using
{% Product in Products %}
<p>{{ Product.name }}</p>
{% endfor %}

How to get Request object inside a Twig Extension in Symfony?

How can one access the Request object inside Twig Extension?
namespace Acme\Bundle\Twig;
use Twig_SimpleFunction;
class MyClass extends \Twig_Extension
{
public function getFunctions()
{
return array(
new Twig_SimpleFunction('xyz', function($param) {
/// here
$request = $this->getRequestObject();
})
);
}
public function getName() {
return "xyz";
}
}
As requested in the comments, here's the prefered way of injecting a request into any service. It works with Symfony >= 2.4.
Injecting the request and putting our service in the request scope is no longer recommended. We should use the request stack instead.
namespace AppBundle\Twig;
use Symfony\Component\HttpFoundation\RequestStack;
class MyClass extends \Twig_Extension
{
private $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function getFunctions()
{
$requestStack = $this->requestStack;
return array(
new \Twig_SimpleFunction('xyz', function($param) use ($requestStack) {
$request = $requestStack->getCurrentRequest();
})
);
}
public function getName()
{
return "xyz";
}
}
app/config/services.yml
app.twig_extension:
class: AppBundle\Twig\MyExtension
arguments:
- '#request_stack'
tags:
- { name: twig.extension }
Docs:
the request stack API
the request stack announcement
Register your extension as a service and give it the container service:
# services.yml
services:
sybio.twig_extension:
class: %sybio.twig_extension.class%
arguments:
- #service_container
tags:
- { name: twig.extension, priority: 255 }
Then retrieve the container by your (twig extension) class constructor and then the request:
<?php
// Your class file:
// ...
class MyClass extends \Twig_Extension
{
/**
* #var ContainerInterface
*/
protected $container;
/**
* #var Request
*/
protected $request;
/**
* Constructor
*
* #param ContainerInterface $container
*/
public function __construct($container)
{
$this->container = $container;
if ($this->container->isScopeActive('request')) {
$this->request = $this->container->get('request');
}
}
// ...
Note that testing the scope is usefull because there is no request when running console command, it avoids warnings.
That's it, you are able to use the request !
I would suggest setting 'needs_environment' => true for your Twig_SimpleFunction, which then will add \Twig_Environment as first argument of your function. Then in your function you can find the request like this:
$request = $twig->getGlobals()['app']->getRequest();
So the whole function will look like this:
...
public function getFunctions() {
return [
new \Twig_SimpleFunction('xyz', function(\Twig_Environment $env) {
$request = $twig->getGlobals()['app']->getRequest();
}, [
'needs_environment' => true,
]),
];
}
...

Resources