There is no license information available for the latest version (dev-master) of this package.

dev-master 2015-04-21 11:41 UTC

This package is not auto-updated.

Last update: 2024-11-23 18:09:08 UTC


README

#Commode: Filters

Build Status Code Climate Coverage Status

_laravel-commode/filters is a laravel 4.2 Filter Layer helper, which allows you to organize your routing filters in classes.


####Contents

##Installing

You can install laravel-commode/common using composer:

"require": {
    "laravel-commode/filters": "dev-master"
}

To enable package you need to register LaravelCommode\Filters\FiltersServiceProvider service provider:

    <?php
        // ./yourLaravelApplication/app/config/app.php
        return [
            // ... config code
            'providers' => [
                // ... providers
                'LaravelCommode\Filters\FiltersServiceProvider'
            ]
        ];

##Creating a filter group

To create a filter group you need to create a FilterGroupClass, that must be extended from LaravelCommode\Filters\Groups\AbstractFilterGroup. Each FilterGroupClass needs to implement getPrefix() method, that would return prefix, that is common for methods that are going to be registered as filters. For example, let's say we need an ACL filter group, that could be checking whether user is guest or not, and check if current authorized user has permissions for actions. We will create ACLFilters class, that extends LaravelCommode\Filters\Groups\AbstractFilterGroup and implement getPrefix() method, that would return 'auth' value, since methods' names we're gonna need as filters are prefixes with this string:

    <?php
        namespace Application\Http\Filters;
        
        use LaravelCommode\Filters\Groups\AbstractFilterGroup;
        use Illuminate\Auth\Guard;
        use Illuminate\Foundation\Application;
        
        class ACLFilters extends AbstractFilterGroup
        {
            /**
            * @var \Illuminate\Auth\Guard 
            **/
            private $authGuard;
            
            /**
            * All constructors are passed into DI-container.
            *
            * But, since \Illuminate\Auth\Guard is registered as shared 
            * view name auth, it can not be injected directly, so in 
            * this example I will use Application injection
            **/
            public function __construct(Application $application)
            {
                $this->authGuard = $application->make('auth'); // grab auth guard from DI 
            }
            
            /**
            * Guest filter
            **/
            public function authGuest()
            {
                if ($this->authGuard->guest()) {
                    return \Redirect::to('security/signin');
                }
            }
            
            /**
            * Dashboard filter
            **/
            public function authDashboard()
            {
                if ($this->authGuest() && !$this->authGuard->user()->hasPermission('dashboard')) {
                    return \Redirect::to('security/signin');
                }
            }
            
            public function getPrefix()
            {
                return 'auth';
            }
        }

##Filter registry

Filter registry is a class that helps you to register your filter groups in laravel environment. It can be accessed through FilterRegistry facade, through laravel's IoC as 'common.filters' or by passing LaravelCommode\Filters\Interfaces\IFilterRegistry into it.

To register your filter group you need to pass it's class name into FilterRegistry::extract($classname) method or if you want to register multiple filter groups you need to use FilterRegistry::extractArray(array $classnames = []); if you have constructed filter group yourself, you can pass it's instance into FilterRegistry::add(\LaravelCommode\Filters\Interfaces\IFilterGroup $filterGroup).

    <?php
        FilterRegistry::extract('Application\Http\Filters\ACLFilters');
        FilterRegistry::extractArray([Application\Http\Filters\ACLFilters::class]); // php 5.5 style
        FilterRegistry::add(new Application\Http\Filters\ACLFilters(app()))