2017 © Pedro Peláez
 

library laravel-middleware

Laravel 4 Middleware Loader

image

slushie/laravel-middleware

Laravel 4 Middleware Loader

  • Friday, November 22, 2013
  • by slushie
  • Repository
  • 1 Watchers
  • 1 Stars
  • 748 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

middleware

Laravel 4 Middleware Configuration Loader, (*1)

This package provides a simple configuration point where you can add middleware to the Laravel 4 request processing pipeline., (*2)

It works by adding callbacks to the App::before() and App::after() application-level events. Classes are registered in the app.middleware configuration key array (that is, middleware is an array defined in the app/config/app.php file)., (*3)

How To Use

First, create a class that implements at least one of Slushie\Middleware\BeforeInterface or Slushie\Middleware\AfterInterface. Then implement the onBefore($request) and onAfter($request, $response) methods. You can return a non-null value from an onBefore handler to halt request processing, just like with Laravel's App::before(). Return values for onAfter() are ignored., (*4)

class RedirectBeforeMiddleware implements BeforeInterface {
  public function onBefore($request) {
    if ($request->query('mobile') == 'redirect') {
      return Redirect::to('/mobile');
    }
  }
}

class TimingMiddleware implements BeforeInterface, AfterInterface {
  public function onBefore($request) {
    $this->start_time = time();
  }

  public function onAfter($request, $response) {
    $duration = time() - $this->start_time;
    Log::info("Processing {$request->url()} took {$duration} sec");
  }
}

Next, configure your application to load the MiddlewareServiceProvider as well as adding your own middleware classes to the middleware configuration key., (*5)

For example, your app/config/app.php file might include:, (*6)

'providers' => array(
  // .. snip ..

  'Slushie\Middleware\MiddlewareServiceProvider'
),

'middleware' => array(
  'RedirectBeforeMiddleware',
  'TimingMiddleware'
)

How It Works

The MiddlewareServiceProvider creates objects from all classes in the app.middleware array using the IoC container. Any class that implements BeforeInterface is used as before middleware, and any class that implements AfterInterface is used as an after middleware. Of course, a class can implement both interfaces to be used at both control points., (*7)

Rationale

Although Laravel provides a simple method to apply middleware in each application via the App::before() and App::after() methods, this approach does not lend itself to simple configuration., (*8)

This means that packages that need to apply middleware to the requests must do so in their service provider instances, which enforces this behaviour in user applications., (*9)

This package provides a means for application developers and administrators to selectively enable and disable middleware without resorting to editing application code (such as app/filters.php)., (*10)

The Versions

22/11 2013

dev-master

9999999-dev

Laravel 4 Middleware Loader

  Sources   Download

The Requires

 

by Josh Leder