I'm building a site where the user can choose a country, state and city he wants.
Once he selects these parameters he goes to a page like this: en.example.com/spain/madrid/madrid/
The problem is, every time I want to build a new url, I must pass these 3 variables and I was wondering if I could do something to make them just like the _locale variable which symfony itself passes it to the parameters.
Thanks
After searching more I found this post: http://blog.viison.com/post/15619033835/symfony2-twig-extension-switch-locale-current-route
I just used the idea and made the changes I needed and this is the final code for my extension
<?php
namespace Comehoy\CoreBundle\Twig\Extension;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernel;
class PathExtension extends \Twig_Extension
{
private $request;
private $router;
public function __construct(Router $router) {
$this->router = $router;
}
public function onKernelRequest(GetResponseEvent $event) {
if ($event->getRequestType() === HttpKernel::MASTER_REQUEST) {
$this->request = $event->getRequest();
}
}
public function getFunctions()
{
return array(
'l10n_path' => new \Twig_Function_Method($this, 'getPath')
);
}
public function getPath($name, $parameters = array())
{
$parameters = array_merge($parameters, [
'country' => $this->request->get('country'),
'state' => $this->request->get('state'),
'city' => $this->request->get('city'),
]);
return $this->generator->generate($name, $parameters, false);
}
public function getName()
{
return 'twig_my_path_extension';
}
}
And as for the configuration its the same as the post
services:
twig.localized_route_extension:
class: Acme\CoreBundle\Twig\PathExtension
tags:
- { name: twig.extension }
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
arguments: [#router]
And for the routes that I use country, state and the city I put them in the prefix to avoid repeating them in each route.
acme_core:
resource: "#AcmeCoreBundle/Controller/"
type: annotation
prefix: /{country}/{state}/{city}
Hope it helps someone else.
Related
I want to set session value in Symfony2 view both in PHP and TWIG . I can get the session value in view like this
$app->getSession()->get('whatever');
But didn't know how to set the session value in view . Kindly any one help .
This is your twig extension you can put this code under YourBundle/Twig/SessionExtension.php
namespace YourBundle\Twig;
use Symfony\Component\HttpFoundation\Session\Session;
class SessionExtension extends \Twig_Extension
{
private $session;
public function __construct(Session $session) {
$this->session = $session;
}
public function getFilters()
{
return array(
new \Twig_SimpleFilter('setSession', array($this, 'setSession')),
);
}
public function setSession($key, $val){
$this->session->set($key, $val);
return true;
}
public function getName()
{
return 'session_extension';
}
}
Add to service for twig;
yourbundle.twig.session_extension:
class: YourBundle\Twig\SessionExtension
arguments:
session: "#session"
tags:
- { name: twig.extension }
Now , you can use in twig;
{{ 'test'|setSession('myKey', 'myValue') }}
I have problems with adding Twig extensions.
I have Bundle controllers extending custom BaseController class:
class DefaultController extends BaseController
And there's my BaseController class (only part of it).
class BaseController extends Controller {
public function setContainer(\Symfony\Component\DependencyInjection\ContainerInterface $container = null)
{
parent::setContainer($container);
$this->onContainerSet();
}
public function onContainerSet()
{
// many other tasks
$this->get('twig')->addExtension(new \Twig_Extension_StringLoader());
$this->get('twig.loader')->addPath('../app');
$function = new \Twig_SimpleFunction('stars', function ($number, $maximum_stars = 5) {
$this->get('twig')->addGlobal('star_number',sprintf("%.1f",$number));
$this->get('twig')->addGlobal('star_max',$maximum_stars);
$full_stars = floor($number);
$half_stars = ($number - $full_stars) * 2;
$empty_stars = $maximum_stars - $full_stars - $half_stars;
$this->get('twig')->addGlobal('full_stars_number',$full_stars);
$this->get('twig')->addGlobal('half_stars_number',$half_stars);
$this->get('twig')->addGlobal('empty_stars_number',$empty_stars);
echo $this->renderView(
'views/stars.html.twig'
);;
});
$function2 = new \Twig_SimpleFunction('inurl', function ($anchor, $code) {
echo ''.$anchor."";
});
$this->get('twig')->addFunction($function);
$this->get('twig')->addFunction($function2);
}
}
The problem:
When I clear cache directory I have first message:
CRITICAL - Uncaught PHP Exception LogicException: "Unable to register
extension "string_loader" as extensions have already been
initialized." at ...\vendor\twig\twig\lib\Twig\Environment.php line
660 Context: {"exception":"Object(LogicException)"}
But when I reload page (cache folder is already created) it works fine (no exception).
However if I comment line:
// $this->get('twig')->addExtension(new \Twig_Extension_StringLoader());
and clear cache directory I have exception:
CRITICAL - Uncaught PHP Exception LogicException: "Unable to add
function "stars" as extensions have already been initialized." at
...\vendor\twig\twig\lib\Twig\Environment.php line 946 Context:
{"exception":"Object(LogicException)"}
So it seems that when cache directory doesn't exist from some reason adding any Twig extensions doesn't work (extensions have already been initialized) as I would like but when cache directory is already created everything works fine.
Question - how to solve it in the simplest way?
Create your class in YourBundle\Twig
class YourExtension extends \Twig_Extension
{
/**
* #var Router
*/
protected $router;
function __construct(Router $router)
{
$this->router = $router;
}
/**
* #return array
*/
public function getFilters()
{
return [
new \Twig_SimpleFilter('my_filter', [$this, 'myFilter'], ['is_safe' => ['html']]),
];
}
/**
* #return string
*/
public function myFilter(User $user)
{
return 'FILTERED: ' . $user->getName();
}
/**
* #return string
*/
public function getName()
{
return 'my_filter_extension';
}
}
Then, register your extension as a service: ( in this case I inject router as an argument )
yourbundle.twig.my_filter_extension:
class: Acme\YourBundle\Twig\YourExtension
arguments: [#router]
tags:
- { name: twig.extension }
If you want to enable Twig_Extension_StringLoader, add to your services:
yourbundle.twig.extension.loader:
class: Twig_Extension_StringLoader
tags:
- { name: 'twig.extension' }
Twig_Extension_StringLoader is not loaded by default.
What I finally did to achieve result (maybe someone will have similar problem in the future):
In config.yml I've added:
services:
yourbundle.twig.extension.loader:
class: Twig_Extension_StringLoader
tags:
- { name: 'twig.extension' }
yourbundle.twig.stars_extension:
class: Mnab\Twig\Stars
tags:
- { name: 'twig.extension' }
yourbundle.twig.inurl_extension:
class: Mnab\Twig\InternalUrl
tags:
- { name: 'twig.extension' }
in my BaseController I only left from question code:
$this->get('twig.loader')->addPath('../app');
but also added:
$this->get('twig')->addGlobal('internal_links',$this->internalLinks);
to use it in Twig extension
And I've create 2 classes:
<?php
//InternalUrl.php
namespace Mnab\Twig;
use Symfony\Component\DependencyInjection\ContainerInterface;
class InternalUrl extends \Twig_Extension {
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('inurl', array($this, 'inUrlFunction'), array('needs_environment' => true, 'is_safe' => array('html'))),
);
}
public function inUrlFunction(\Twig_Environment $env, $anchor, $code)
{
return ''.$anchor."";
}
public function getName()
{
return 'inurl_extension';
}
}
and
<?php
// Stars.php
namespace Mnab\Twig;
class Stars extends \Twig_Extension
{
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('stars', array($this, 'starsFunction'), array('needs_environment' => true, 'is_safe' => array('html'))),
);
}
public function starsFunction(\Twig_Environment $env, $number, $maximum_stars = 5)
{
$env->addGlobal('star_number',sprintf("%.1f",$number));
$env->addGlobal('star_max',$maximum_stars);
$full_stars = floor($number);
$half_stars = ($number - $full_stars) * 2;
$empty_stars = $maximum_stars - $full_stars - $half_stars;
$env->addGlobal('full_stars_number',$full_stars);
$env->addGlobal('half_stars_number',$half_stars);
$env->addGlobal('empty_stars_number',$empty_stars);
return $env->render(
'views/stars.html.twig'
);
}
public function getName()
{
return 'stars_extension';
}
}
Now it seems to work regardless of cache is created or not. So it seems to better register services when you want to use Twig Extensions than registering Extensions in Controller.
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 }
I'm getting an error:
"twig extension FatalErrorException: Error: Class Acme\Bundle\MyBundle\Twig not found in app/cache/dev/appDevDebugProjectContainer.php"
I've cleared the cache but this does nothing.
I'm just trying to test setting it up and then I can put in all my logic.
--
A file named MyTwigExtensions.php
namespace Acme\Bundle\MyBundle\Twig;
class MyTwigExtensions extends \Twig_Extension
{
public function getFunctions() {
return array(
new Twig_SimpleFunction('link', 'generate_link')
);
}
public function generate_link($params) {
return "THE-LINK-HERE";
}
public function getName() {
return "link";
}
}
In services.yml
services:
my_extension.twig.extension:
class: Acme\Bundle\MyBundle\Twig
arguments: []
tags:
- { name: twig.extension }
You have to enter fully qualified name of the extension class.
services:
my_extension.twig.extension:
class: Acme\Bundle\MyBundle\Twig\MyTwigExtensions # <--- here
arguments: []
tags:
- { name: twig.extension }
for service reprensetation format is sth like this. addArrowInCode below is the name of the method used in twig:
twig.extension.addArrowInCode:
class: Acme\DemoBundle\Twig\AddArrowInCodeExtension
tags:
- { name: twig.extension }
and for this extension you should have like...
class AddArrowInCodeExtension extends \Twig_Extension
{
function addArrowInCodeFilter($code, $separator = '⇒')
{
// do sth setting final
return $final;
}
/**
* Returns a list of filters to add to the existing list.
*
* #return array An array of filters
*/
public function getFilters()
{
return array(
'addArrowInCode' => new Twig_Filter_Method($this, 'addArrowInCodeFilter', array('needs_environment' => false)),
);
}
public function getName()
{
return 'addArrowInCode';
}
}
hope it helps
Please check this code.
namespace Acme\Bundle\MyBundle\Twig;
class MyTwigExtensions extends \Twig_Extension
{
public function getFunctions() {
return array(
new Twig_SimpleFunction('link', array($this, 'generate_link')) // <== changed here
);
}
public function generate_link($params) {
return "THE-LINK-HERE";
}
public function getName() {
return "link";
}
}
In services.yml
services:
my_extension.twig.extension:
class: Acme\Bundle\MyBundle\Twig\MyTwigExtensions # <== changed here
arguments: []
tags:
- { name: twig.extension }
I've an abstract CRUDController extending Controller. In my newAction, os success, i'd like to redirect to showAction($slug) using redirect method:
return $this->redirect($this->generateUrl($route, $params));
But newAction is actually called in the subclass UserController, so i can't specify route name $route in my CRUDController.
class UserController extends CRUDController { }
abstract class CRUDController extends Controller
{
/** #Template */
public function showAction($slug) { }
/** #Template */
public function newAction(Request $request)
{
$model = $this->createModel();
$form = $this->createForm($this->createType(), $model);
if('GET' == $request->getMethod())
return array('form' => $form->createView());
$form->bindRequest($request);
if(!$form->isValid()) return array(
'errors' => $this->get('validator')->validate($model),
'form' => $form->createView()
);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($model);
$em->flush();
// Success, redirect to showAction($slug)
}
}
An example of routes:
users_show:
pattern: /users/show/{slug}
defaults: { _controller: AcmeSecurityBundle:User:show }
requirements:
_method: GET
users_new:
pattern: /users/new
defaults: { _controller: AcmeSecurityBundle:User:new }
requirements:
_method: GET
users_create:
pattern: /users/new
defaults: { _controller: AcmeSecurityBundle:User:new }
requirements:
_method: POST
You can work with the whole concept of OO and have an interface method called getRouteName() in your abstract class:
abstract public function getRoute();
And then, on your concrete class, or subclass, UserController, you just override and implement that:
public function getRoute()
{
return 'whatever:Route:YouWant';
}
So when, on your abstract class, call the actual interface method, the OO will handle everything like magic:
public function newAction(Request $request)
{
...
return $this->redirect($this->generateUrl($this->getRouteName(), $params));
}
Maybe try that and let us know if does the job right.
I sometimes create pseudo-interface when I need to do actions like this in sf:
interface specialRedirect
{
public function specialRedirect($slug);
}
So then you could do something like this in your abstract class:
public function newAction(Request $request)
{
// ...
$this->specialRedirect('user', $slug);
}
public function specialRedirect($slug)
{
// If you don't want to do anything special, just act like normal
// Success, redirect to showAction($slug)
}
But then in UserController you extend/implement the interface to your liking:
public function specialRedirect($slug)
{
//here is where you can specify your route name.
$route = 'Foo_Bar';
return $this->redirect($this->generateUrl($route, $params));
}