laravel-commode / filters
Requires
- php: >=5.4.0
- illuminate/routing: 4.2.*
- illuminate/support: 4.2.*
- laravel-commode/common: dev-master
Requires (Dev)
- satooshi/php-coveralls: dev-master
This package is not auto-updated.
Last update: 2024-11-23 18:09:08 UTC
README
#Commode: Filters
_laravel-commode/filters is a laravel 4.2 Filter Layer helper, which allows you to organize your routing filters in classes.
####Contents
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' ] ];
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 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()))