Call from the team

I want to execute an action from a user command. The controller.php class best method provides this function, but I don’t know how to access it from the execute () method of the batch file

+4
source share
3 answers

Bringing the controller to the service will lead to performance overhead due to the request stack, since then it should return HttpResponse. The area Requestwill not be very useful in a team.

I would advise you to reorganize the action from the controller to a separate definition of the service class and get this class from the container both in the controller and in the team by making it ContainerAwareCommand.

http://symfony.com/doc/current/cookbook/console/console_command.html#getting-services-from-the-service-container

+5

+3

, ( ) .

Symfony\Bundle\FrameworkBundle\Controller, , forward() Request, HttpKernel. HttpKernel, .

, , :

$controller = new Your\Controller();
$controller->yourAction();

, :

$request = Symfony\Component\HttpFoundation\Request::createFromGlobals();
$controller = new Your\Controller();
$controller->setContainer($this->getContainer()); // from the ContainerAwareCommand interface
$controller->yourAction($request);

, , :

  • Include your controller in the service, this is described in the cookbook and will mean that your controller and action can be called directly from the container that your command is (possibly) configured. This manages the dependencies your controller and actions may have.
  • Separate the functionality you need from your action into your class or service. This is the best way forward, as if you were invoking the same bit of code in two places, it makes sense to centralize it somewhere.
+1
source

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


All Articles