Like the title says, I would like to use the same controller, but different views, based on the HTTP host name. Is this possible? What would be the best architecture to accomplish it?
If the controller returns null then the Symfony 2 request handler will dispatch a KernelEvents::VIEW event.
You can make yourself a view listener (http://symfony.com/doc/current/cookbook/service_container/event_listener.html) to catch the event. Your view listener would then need the logic to determine which view to create based on request parameters such as the host name. The view would then create the response object. The listener then sets the response in the event.
Is this the "best" approach. Hard to say. There is no reason why the controller itself could not create the view. On the other hand, with a view listener you can share views with multiple controllers. Really depends on your application.
Here is an example of a view listener which kicks off different views depending on the _format attribute.
namespace Cerad\Bundle\CoreBundle\EventListener;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ViewEventListener extends ContainerAware implements EventSubscriberInterface
{
const ViewEventListenerPriority = -1900;
public static function getSubscribedEvents()
{
return array(
KernelEvents::VIEW => array(
array('onView', self::ViewEventListenerPriority),
),
);
}
/* =================================================================
* Creates and renders a view
*/
public function onView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
if ($request->attributes->has('_format'))
{
$viewAttrName = '_view_' . $request->attributes->get('_format');
}
else $viewAttrName = '_view';
if (!$request->attributes->has($viewAttrName)) return;
$viewServiceId = $request->attributes->get($viewAttrName);
$view = $this->container->get($viewServiceId);
$response = $view->renderResponse($request);
$event->setResponse($response);
}
# services.yml
cerad_core__view_event_listener:
class: '%cerad_core__view_event_listener__class%'
calls:
- [setContainer, ['#service_container']]
tags:
- { name: kernel.event_subscriber }
# routing.yml
cerad_game__project__schedule_team__show:
path: /project/{_project}/schedule-team.{_format}
defaults:
_controller: cerad_game__project__schedule_team__show_controller:action
_model: cerad_game__project__schedule_team__show_model_factory
_form: cerad_game__project__schedule_team__show_form_factory
_template: '#CeradGame\Project\Schedule\Team\Show\Twig\ScheduleTeamShowPage.html.twig'
_format: html
_view_csv: cerad_game__project__schedule_team__show_view_csv
_view_xls: cerad_game__project__schedule_team__show_view_xls
_view_html: cerad_game__project__schedule_team__show_view_html
requirements:
_format: html|csv|xls|pdf
Related
I use an event subscriber to handle some actions when my order form is submitted.
Problem my event is not being dispached but symfony is able to find him because he tells me that my OrderEvent::ORDER_CREATE is orphan.
I excpected that execution was stopped with die('Hello you from subscriber'); but it's not.
Controller
public function commanderPanierAction(Request $request, SelectionWeb $selectionWeb, TableLumineuse $tableLumineuse, EventDispatcherInterface $eventDispatcher)
{
// DO PREVIOUS STUFF
$Order = new Order();
$OrderForm = $this->createForm(OrderForm::class, $Order);
if ($request->isMethod('POST')) {
$OrderForm->handleRequest($request);
if ($OrderForm->isSubmitted() && $OrderForm->isValid()) {
// OrderForm is valid proceed
$eventDispatcher->dispatch(
new OrderEvent($Order),
OrderEvent::ORDER_CREATE
);
}
}
OrderEvent
<?php
namespace App\Event;
use App\Entity\Order;
use Symfony\Contracts\EventDispatcher\Event;
class OrderEvent extends Event
{
public const ORDER_CREATE = 'order.created';
protected $order;
public function __construct(Order $order)
{
$this->order= $order;
}
public function getOrder(): Order
{
return $this->order;
}
}
OrderSubscriber
<?php
namespace App\EventSubscriber;
use App\Event\CommandeWebEvent;
use App\Service\SelectionWeb\SelectionWeb;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OrderSubscriber implements EventSubscriberInterface
{
private $entityManager;
private $selectionWeb;
public function __construct(EntityManagerInterface $entityManager, SelectionWeb $selectionWeb)
{
$this->entityManager = $entityManager;
$this->selectionWeb = $selectionWeb;
}
public static function getSubscribedEvents()
{
return [
OrderEvent::ORDER_CREATE => [
// The higher the number, the earlier the method is called.
['processOrder', 10],
['notifyOrder', -10]
]
];
}
public function processOrder(OrderEvent $event)
{
// TODO
die('Hello you from subscriber');
}
public function notifyOrder(OrderEvent $event)
{
// TODO
}
}
EDIT
The only workaround found (thx to #nikserg) is to inject subscriber into controller action (subscriber has dependencies) then register my subscriber as service in services.yaml finaly use $eventDispatcher->addSubscriber($subscriber); before $eventDispatcher->dispatch(new OrderEvent($Order),OrderEvent::ORDER_CREATE);
It seems all that stuff is really complex for a task as simple as that
EDIT2
I found an another way I'm able to execute my subscriber without usage of $eventDispatcher->addSubscriber($subscriber); and only with $eventDispatcher->dispatch(new OrderEvent($Order)); only if I configure my subscriber as service in services.yaml but why symfony does need this information in services.yaml ? Thought that everything in src/ is avaible to be used as service..
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
# If I add those It works
App\EventSubscriber\OrderSubscriber:
autowire: true
EDIT3
My OrderSubscriber is loaded into container so why I should set it explicitly to being execute ? I can't figure out what's going on
php bin/console debug:container
---------------------------------------- ---------------------------------------
Service ID Class name
---------------------------------------- ----------------------------------------
App\EventSubscriber\OrderSuscriber App\EventSubscriber\OrderSuscriber
EDIT 4
If I set my OrderSubscriber explicitly there is two instances of it into container.
Why symfony execute one set explicitly and not the one set with resource: '../src/*'
Symfony will autowire your subscriber as service, if you will require it as argument in action:
public function commanderPanierAction(Request $request, SelectionWeb $selectionWeb, TableLumineuse $tableLumineuse, OrderSubscriber $orderSubscriber)
Of course, if your subscriber is registered properly.
But let me advice you not to create subscribers as objects manually. The main good thing about subscribers is that you know nothing about them, when you fire event. There could be dozens of subscribers to this event, and all of them will proceed your event. That will keep your code nice and lower cohesion.
It's in docs: https://symfony.com/doc/current/event_dispatcher.html#creating-an-event-subscriber
First I want to thank you all for your time and let me apologize my problem was due to a typo I wrote OrderSuscriber instead of OrderSubscriber that's why there was 2 services into my container and why defined service explicitly was working.
I have a catch-all fallback route in Symfony2 that I couldn't get to work in Symfony3. I tried this exact syntax (a verbatim copy of my Symfony2 route) and that didn't work.
fallback:
path: /{req}
defaults: { _controller: MyBundle:Default:catchAll }
requirements:
req: ".+"
How can I get this working in Symfony3? (It's literally the only thing holding me back from using Symfony3 and keeping me at v2.8)
This should help you:
route1:
path: /{req}
defaults: { _controller: 'AppBundle:Default:index' }
requirements:
req: ".+"
Where, my controller is called "DefaultController", and I have a function called "indexAction()".
Here is my code for the DefaultController:
class DefaultController extends Controller
{
/**
* #Route("/", name="homepage")
*/
public function indexAction(Request $request)
...
I actually did try what you said in my environment, and it didn't work until I had the right controller settings specified.
EDIT:
For this to work, it was necessary to add the parameter Request $request (with the type hint) to the action's method signature.
I found the current accepted answer almost useful for Symfony 4, so I'm going to add my solution:
This is what I did to get it working in Symfony 4:
Open /src/Controller/DefaultController.php, make sure there is a function called index(){}
It's not required to add the Request $request as first param as some comment suggest.
This is the method that will handle all urls caught by the routes.yaml
Open /config/routes.yaml, add this:
yourRouteNameHere:
path: /{req}
defaults: { _controller: 'App\Controller\DefaultController::index' }
requirements: # the controller --^ the method --^
req: ".*"` # not ".+"
You can also override Exception controller.
# app/config/config.yml
twig:
exception_controller: app.exception_controller:showAction
# app/config/services.yml
services:
app.exception_controller:
class: AppBundle\Controller\ExceptionController
arguments: ['#twig', '%kernel.debug%']
namespace AppBundle\Controller;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ExceptionController
{
protected $twig;
protected $debug;
public function __construct(\Twig_Environment $twig, $debug)
{
$this->twig = $twig;
$this->debug = $debug;
}
public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null)
{
// some action
return new Response($this->twig->render('error/template.html.twig', [
'status_code' => $exception->getStatusCode()
]
));
}
}
I have an action which takes an argument, the route looks like this:
/cs/{id}
It's the individualAction in the Cassette controller.
Sometimes, there's an exception 500 Internal Server Error - NoResultException - this is expected behaviour.
I am looking to redirect to another controller action, editAction, when this happens, the route is as such:
/cs/{id}/edit
It needs to be controller-specific, since I want to repeat this with different controller actions. It also needs to keep the argument from the original action.
I've been looking into event listeners, but I'm not sure if that's overkill and I'm struggling to find out how to make them action-specific - I'm happy to keep the code in the controller if that's the better solution.
An EventListener is the most appropriated to change the comportment of your app when an exception is thrown.
Use something like :
<?php
namespace AppBundle\EventListener;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\Routing\RouterInterface;
use Doctrine\ORM\NoResultException;
class ExceptionResponseListener
{
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
/**
* #param GetResponseForExceptionEvent $event
*/
public function onKernelResponse(GetResponseForExceptionEvent $event)
{
$request = $event->getRequest();
$routeName = $request->get('_route');
$exception = $event->getException();
// Restrict the route by adding a check on the route name
// Or a pattern of route names (!strpos($routeName, '_show'))
// Or for many routes: (!in_array($routeName, ['xxx_show', ...]
// Or the _controller: $request->get('_controller')
// Output something like: "AcmeBundle\\Controller\\DefaultController::showAction"
if ('expected_route_name' !== $routeName) {
return;
}
// Retrieve your param
$params = $request->get('_route_params');
if ($exception instanceof NoResultException) {
// Create a redirection to the edit route
$response = new RedirectResponse(
$this->router->generate('your_edit_route', array('id' => $params['id']))
);
$event->setResponse($response);
}
}
}
And register it as service :
services:
# ...
acme.kernel.listener.exception_listener:
class: AppBundle\EventListener\ExceptionResponseListener
tags:
- {name: kernel.event_listener, event: kernel.exception, method: onKernelResponse}
arguments: ['#router']
Another simple alternative could be to make a forward/redirection of the expected action directly in your method, instead of throw the exception.
I am currently using the Symfony2 event listener to change the controller to a different one based on a users authentication status. I get the listener to set the new controller but it is instantiated without the container parameter (i.e. $this->container returns null).
Is there anyway to pass the container on to the controller I am changing to?
class AuthenticationListener
{
public function onController(FilterControllerEvent $event)
{
$request = $event->getRequest();
$session = $request->getSession();
if (!$session->has('authenticated') || $session->get('authenticated') === false)
{
$controller = $event->getController();
if (!($controller[0] instanceof AuthenticateController) && !($controller[0] instanceof ExceptionController))
{
$event->setController(array(new AuthenticateController(), 'loginAction'));
}
}
}
}
The container is not set, when you create the controller automatically. Call setContainer after constructing the controller. Afterwards you can pass it to the event.
In this case AuthenticationListener its just a class
if you want to use $this->container in this class you must do like this:
class BeforeControllerListener extends ContainerAware
{
...
}
and in config.yml
core.listener.before_controller:
class: App\YourBundle\EventListener\YourListener
tags: [ {name: kernel.event_listener, event: kernel.controller, method: onKernelController}]
calls:
- [ setContainer,[ #service_container ]]
Is there a way on Symfony2 to call a controller function on each page load? At the momment my solution is using an ajax call, but i'll like to solve this all in the backend part. (ofcourse without having to copy the function name on each controller function)
You can create Event Listener and handle KernelEvents::CONTROLLER event with it (before filter), as described here.
Example:
Acme\DemoBundle\EventListener\DemoListener.php
namespace Acme\DemoBundle\EventListener;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
class DemoListener
{
public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();
/*
* $controller passed can be either a class or a Closure.
* This is not usual in Symfony2 but it may happen.
* If it is a class, it comes in array format
*/
if (!is_array($controller)) {
return;
}
$controller[0]->fooBarMethod();
}
}
Acme\DemoBundle\Resources\services.yml
parameters:
acme_demo.event_listener.class: Acme\DemoBundle\EventListener\DemoListener
services:
acme_demo.event_listener:
class: %acme_demo.event_listener.class%
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }