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.
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.
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:
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:
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.
$this->forward
takes arguments in this order:
These parameters can be accessed in the controller using request access functions.
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. :)
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