Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 - Call controller from another controller

I would like use a method of controller from another bundle, in my controller.

The method this->forward need a Response object, and i don't know how to use it.

public function indexAction($name)
{
$response = $this->forward('AcmeHelloBundle:Hello:fancy', array(
    'name'  => $name,
    'color' => 'green',
));

// ... further modify the response or return it directly

return $response;
}

And i saw that i can use service but i want to know if its the best solution or they are another.

like image 816
shmoolki Avatar asked Oct 26 '14 13:10

shmoolki


People also ask

How to retrieve the input from a command in Symfony controller?

From the execution of a command you can decide if you retrieve the input or not: With output. To execute a command within a controller, use the following code: The previous controller will return as response "My Third Symfony command ============ First line value : Hello Second line value : World" in the browser.

Does Symfony support MVC?

as long as Symfony has things like " { { render (controller (...)) }}"" MVC shouldn't be a concern If you don't want to define the class as a service, as it doesn't feel as a good practice to me and @Qoop quoted Fabien saying the same, you can use forwarding:

How do I forward to another controller internally?

Though not very common, you can also forward to another controller internally with the forward () method provided by the AbstractController class. Instead of redirecting the user's browser, this makes an "internal" sub-request and calls the defined controller. The forward () method returns the Response object that is returned from that controller:

How to get a service from one controller to another?

You can define your controller as service, then get it in another controller. In your services.yml define needed controller as a service: Then in any controller you'll be able to get this service via container: There is some useful information about Controllers as Services in documentation Is this the best way to do it.


2 Answers

$this->forward takes arguments in this order:

  1. Logical Name of controller action in string format i.e. 'AcmeHelloBundle:Hello:fancy'
  2. Parameters to be passed as request variables in array format i.e. array( 'name' => $name, 'color' => 'green', )

These parameters can be accessed in the controller using request access functions.

like image 101
Apul Gupta Avatar answered Nov 09 '22 15:11

Apul Gupta


Sometimes you want to bypass security completely and run a command in another controller despite a user's permissions level. Luckily you can do that fairly easily.

First, run a use Command for your controller at the top of the controller you want to use the data from:

use AppBundle\Controller\MySourceDataController;

Then call that function from within your destination controller:

$response = MySourceDataController::getTheData( $option1, $option2 );

If you need to pass a Request object, you can do it this way:

$response = MySourceDataController::getTheData( new Request( array(
    'server' => 'USAServer1',
) ), $option2 );

This returns a Request with the set parameter of server. I also defined a $option2, this would be a variable often defined in the URL such as:

* @Route("/mydata/{server}/", name="api-source-data")
* @param Request $request
* @param         $server

Lastly, if you're passing JSON in that controller and want to convert it back to an object, you can run this bit of code on the $response:

if ( 0 === strpos( $response->headers->get( 'Content-Type' ), 'application/json' ) ) {
    $response = json_decode( $response->getContent(), true );
}

Voila. Access any controller form any other controller and bypass security notation for the source controller. :)

like image 37
Dovy Avatar answered Nov 09 '22 15:11

Dovy