I'm following How to Override any Part of a Bundle page on Symfony 2 website. This is interesting:
You can set the parameter holding the service's class name to your own class by setting it in app/config/config.yml. This of course is only possible if the class name is defined as a parameter in the service config of the bundle containing the service.
So i looked at /vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/config
and i found that session.xml
is defining %session.class%
parameter, so it should be easy to extend Symfony Session
class with something like:
namespace Acme\HelloBundle\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\Session;
class ExtendedSession extends Session
{
public function setSuccessFlashText($text, array params = array())
{
parent::setFlash('success', $this->getTranslator()->trans($text, $params);
}
}
I din't tested this yet. But how can i do the same with request
special service? I'd like to adding some convenient shortcuts in order to make my code easier to read.
I found this in services.xml
file:
<!--
If you want to change the Request class, modify the code in
your front controller (app.php) so that it passes an instance of
YourRequestClass to the Kernel.
This service definition only defines the scope of the request.
It is used to check references scope.
-->
<service id="request" scope="request" synthetic="true" />
Here is my app.php
. How i'm supposed to pass an instance of my custom request class?
require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
//require_once __DIR__.'/../app/AppCache.php';
use Symfony\Component\HttpFoundation\Request;
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
//$kernel = new AppCache($kernel);
$kernel->handle(Request::createFromGlobals())->send();
Well, it is simple.
In your app.php
just pass instance of YourRequest
instead of default:
require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
//require_once __DIR__.'/../app/AppCache.php';
use src\YourCompany\YourBundle\YourRequest;
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
//$kernel = new AppCache($kernel);
$kernel->handle(YourRequest::createFromGlobals())->send();
Just ensure, that you extended from default Request
in YourRequest
class.
Should work without additional service definitions.
According to comments, there are thoughts that this will cause problems with IDE autocompletion. In theory - it should not.
In your controller you just add use
statement
use src\YourCompany\YourBundle\YourRequest;
And in action, where you pass $request
, just define its class:
public function yourAction(YourRequest $request)
This will give you autocompletion.
In case if you want to get request as a service or from a controller, for IDE you can also define its class in comment doc:
/** @var $request YourRequest */
$request = $this->getRequest();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With