machy8 / smart-controller
Small abstraction over Symfony AbstractController with useful features.
This package's canonical repository appears to be gone and the package has been frozen as a result.
v1.1.0
2019-04-11 20:00 UTC
Requires
- php: >=7.2
- symfony/framework-bundle: ~4.2
- symfony/twig-bundle: ~4.2
Requires (Dev)
- symplify/easy-coding-standard: ^5.2
- tracy/tracy: ^2.4
README
Smart Controller
Based on the article Symfony 4: Creating Smart Controller. Summary:
- Before render method - allows you to set parameters you always need
- Template parameters - can be set from multiple places easily
- Usefull methods - getRequest(), getRootDirectory(), getTemplateParameter()
Installation
composer require machy8/smart-controller
Example
Symfony original lucky controller example.
class LuckyController extends AbstractController { /** * @Route("/lucky/number") */ public function number(): Response { return $this->render('lucky/number.twig', [ 'number' => random_int(0, 100), ]); } /** * @Route("/unlucky/number") */ public function unluckyNumber(): Response { return $this->render('lucky/number.twig', [ 'number' => random_int(0, 100), ]); } }
and now with the Smart Controller.
class LuckyController extends SmartController { public function beforeRender(): void { $this->setTemplateParameters([ 'number' => random_int(0, 100) ]); } /** * @Route("/lucky/number") */ public function renderLuckyNumber(): Response { return $this->renderTemplate('lucky/number.twig'); } /** * @Route("/unlucky/number") */ public function renderUnluckyNumber(): Response { return $this->renderTemplate('lucky/number.twig'); } }