Symfony 2 query service extension?

I follow How to override any part of a package on the Symfony 2 website. This is interesting:

You can set the parameter containing the service 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 config service of the package containing the service.

So, I looked at /vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/config , and I found that session.xml defines the %session.class% parameter, so it should be easy to extend the 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 have not tested this yet. But how can I do the same with request special service? I would like to add some convenient shortcuts to make my code more readable.

I found this in the 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 should I pass an instance of the user 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(); 
+6
source share
1 answer

Well, that’s easy.

In app.php simply enter an instance of YourRequest instead of the default value:

 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 make sure you extend the standard Request in the YourRequest class.

Should work without additional service definitions.


According to the comments, there are thoughts that this will cause IDE autocompletion problems. Theoretically - it should not be.

In your controller, you simply add a use statement

 use src\YourCompany\YourBundle\YourRequest; 

And in the action where you go through $request , just define its class:

 public function yourAction(YourRequest $request) 

This will give you autocomplete.

If you want to receive the request as a service or controller, for the IDE you can also define your class in the doc comment:

  /** @var $request YourRequest */ $request = $this->getRequest(); 
+13
source

Source: https://habr.com/ru/post/922190/


All Articles