Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
class PagesController extends AbstractController
{
/**
* @Route("/page/{name}", name="my_custom_page")
* @param Request $request
* @param string $name
*
* @return Response
*/
public function show(Request $request, string $name): Response
{
// ...
return $this->render('pages/my-template.html.twig', $params);
}
}
class Demo
{
public function __invoke(string $a, string $b)
{
return $a . ' ' . $b;
}
}
$demo = new Demo;
var_dump($demo('hello', 'world'));
// hello world
namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class MyController extends AbstractController
{
public function __invoke(Request $request): Response
{
// Do stuff
// Send to template
return $this->render('pages/my-template.html.twig', ['param' => $someParam]);
}
}
namespace App\Controller;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Error\Error;
class MyController
{
/** @var Environment */
private $twig;
/**
* @param Environment $twig
*/
public function __construct(Environment $twig)
{
$this->twig = $twig;
}
/**
* @Route("/page/{name}", name="my_custom_page")
* @param Request $request
*
* @return Response
*/
public function __invoke(Request $request): Response
{
// Do stuff
// Send to template
$content = $this->twig->render('pages/my-template.html.twig', ['param' => $someParam]);
return new Response($content);
}
}
ibexa:
system:
site_group:
content_view:
full:
article:
template: '@ezdesign/full/article.html.twig'
controller: App\Controller\Article
match:
Identifier\ContentType: [article]
use eZ\Bundle\EzPublishCoreBundle\Controller;
use eZ\Publish\Core\MVC\Symfony\View\ContentView;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class Article extends Controller
{
/**
* @param Request $request
* @param ContentView $view
*
* @return ContentView
*/
public function __invoke(Request $request, ContentView $view): ContentView
{
$someParameter = '...'; // get data
$view->addParameters(['my_param' => $someParameter]);
$response = new Response();
$view->setResponse($response);
return $view;
}
}
my_invokable_route:
path: /my-invokable-url
default:
_controller: App\Controller\MyController
{{ render(controller('App\\Controller\\MyController')) }}
{{ render_esi(controller('App\\Controller\\MyController::__invoke')) }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment