symfony call controller from controller code example

Example 1: symfony call another controller

// OtherController::fancy($name, $color) will be executed
public function myAction($name)
{
    $response = $this->forward(
    	'App\Controller\OtherController::fancy',
    	array(
      		'name'  => $name,
        	'color' => 'green',
    	)
    );
    // ... further modify the response or return it directly
    return $response;
}

Example 2: Basic Symfony Controller

// src/Controller/LuckyController.php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class LuckyController
{
    /**
     * @Route("/lucky/number/{max}", name="app_lucky_number")
     */
    public function number(int $max): Response
    {
        $number = random_int(0, $max);

        return new Response(
            '<html><body>Lucky number: '.$number.'</body></html>'
        );
    }
}

Tags:

Misc Example