2017 © Pedro Peláez
 

library symfony-init-controller

Runs init controller method before every action

image

yavin/symfony-init-controller

Runs init controller method before every action

  • Monday, May 16, 2016
  • by Yavin
  • Repository
  • 1 Watchers
  • 2 Stars
  • 2,942 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 2 % Grown

The README.md

Symfony Init Controller extension

Build Status, (*1)

Add ability to execute init method in controller before every action. Based on this answer, (*2)

Example

class SampleController extends Controller implements InitControllerInterface
{
    protected $page;

    public function init(Request $request)
    {
        if ($request->get('redirect') == 1) {
            return $this->redirect('http://example.com');
        }

       $this->page = $request->get('page');
    }

    public function indexAction()
    {
        //some action code
    }
}

Instalation

  1. Add library to composer.json, (*3)

    "yavin/symfony-init-controller": "0.3"
    

    and run command, (*4)

    composer update yavin/symfony-init-controller
    
  2. Add service in your bundle services file Resources/config/services.xml:, (*5)

    <service class="Yavin\Symfony\Controller\InitControllerSubscriber">
       <tag name="kernel.event_subscriber"/>
    </service>
    

    or if you have services.yml:, (*6)

    services:
       symfony.controller.subscriber.init:
           class: Yavin\Symfony\Controller\InitControllerSubscriber
           tags:
               - { name: kernel.event_subscriber }
    
  3. Then implement InitControllerInterface in controller that you want to have init method., (*7)

    namespace Acme\DemoBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    use Yavin\Symfony\Controller\InitControllerInterface;
    
    class SampleController extends Controller implements InitControllerInterface
    {
       protected $page;
    
       public function init(Request $request)
       {
           //init method could return response, for example redirect
           if ($request->get('redirect') == 1) {
               return $this->redirect('http://example.com');
           }
    
           $this->page = $request->get('page');
       }
    
       public function indexAction()
       {
           //...
       }
    
       public function otherAction()
       {
           //...
       }
    }
    

The Versions

16/05 2016

dev-master

9999999-dev

Runs init controller method before every action

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Yavin

16/05 2016

v0.4

0.4.0.0

Runs init controller method before every action

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Yavin

30/10 2015

dev-symfony-2

dev-symfony-2

Runs init controller method before every action

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Yavin

30/10 2015

v0.3

0.3.0.0

Runs init controller method before every action

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Yavin

15/08 2014

v0.2

0.2.0.0

Runs init controller method before every action

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Yavin

20/12 2013

v0.1

0.1.0.0

Runs init controller method before every action

  Sources   Download

MIT

The Requires

 

by Avatar Yavin