Skip to content

Instantly share code, notes, and snippets.

@adrienlucas
Created July 20, 2023 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrienlucas/5f832d2aef49012572f6924d5538d611 to your computer and use it in GitHub Desktop.
Save adrienlucas/5f832d2aef49012572f6924d5538d611 to your computer and use it in GitHub Desktop.
`make:controller --standalone --invoke`
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Annotation\Route;
#[AsController]
class WithoutTwigController
{
#[Route('/without/twig', name: 'app_without_twig')]
public function __invoke(): JsonResponse
{
return new JsonResponse([
'message' => 'Welcome to your new controller!',
'path' => 'src/Controller/WithoutTwigController.php',
]);
}
}
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;
#[AsController]
class WithTwigController
{
public function __construct(
private Environment $twig,
) {
}
#[Route('/with/twig', name: 'app_with_twig')]
public function __invoke(): Response
{
return new Response($this->twig->render('with_twig.html.twig', [
'controller_name' => 'WithTwigController',
]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment