PHP code example of dr-benton / before-after-controllers-hooks-bundle

1. Go to this page and download the library: Download dr-benton/before-after-controllers-hooks-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dr-benton / before-after-controllers-hooks-bundle example snippets




namespace AppBundle\Controller;

use DrBenton\Bundle\BeforeAfterControllersHooksBundle\Annotation\BeforeControllerHook as BeforeHook;
use DrBenton\Bundle\BeforeAfterControllersHooksBundle\Annotation\AfterControllerHook as AfterHook;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

/**
 * Before any of this Controller Action, its 'checkSomething' method
 * will be triggered:
 *
 * @BeforeHook("checkSomething")
 */
class BooksController extends Controller
{
    /**
     * The Controller 'checkBooksAvailability()' method will be triggered
     * before this Controller action:
     *
     * @BeforeHook("checkBooksAvailability")
     * @Template()
     */
    public function indexAction()
    {
        $books = $this->getDoctrine()
                    ->getRepository('AppBundle:Book')
                    ->findAll();

        return ['books' => $books];
    }

    /**
     * You can also send params to the triggered hook:
     *
     * @BeforeHook("doSomethingBeforeAction", args={"param1", "param2"})
     * @Template()
     */
    public function showAction(Book $book)
    {
        return ['book' => $book];
    }

    /**
     * Want to trigger a Symfony Service method? No problem!
     * Just use the "@[serviceId]::[methodName]" notation:
     *
     * @BeforeHook("@logger::addInfo", args={"showComments() will be called"})
     * @Template()
     */
    public function showCommentsAction(Book $book)
    {
        return ['book' => $book];
    }

    /**
     * You can also trigger a custom callable after the Controller action:
     *
     * @AfterHook("addDebugCodeAfterAction")
     * @Template()
     */
    public function showSomethingAction(Book $book)
    {
        return ['book' => $book];
    }

    /**
     * You can use Services here too, and use params. Any "%response%" param
     * will be replaced with the Controller's returned Symfony Reponse.
     *
     * @AfterHook("@my_service::doSomethingAfterAction", args={"%response%", {"key" => "value"}})
     * @Template()
     */
    public function showSomethingAction(Book $book)
    {
        return ['book' => $book];
    }

    protected function checkSomething()
    {
        // Do something here...
        // It this method returns a Symfony Response, the Controller
        // will be short-circuited and this Response will be sent to the client.
    }

    protected function checkBooksAvailability()
    {
        // idem: return a Response here if oy want to short-circuit the Controller
    }

    protected function doSomethingBeforeAction($arg1, $arg2)
    {
        // Do something here...
    }

    protected function addDebugCodeAfterAction(Response $controllerResponse)
    {
        if ($this->container->getParameter('debug')) {
            $controllerResponse->setContent(
                $controllerResponse->getContent() .
                '<script src="//assets/js/debug.js"></script>'
            );
        }
    }
}
 php

// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new DrBenton\Bundle\BeforeAfterControllersHooksBundle\BeforeAfterControllersHooksBundle(),
    );
}