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

This package is auto-updated.

Last update: 2024-01-13 03:52:14 UTC


README

Build Status

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');
    }

}