Laravel5.0-php Tutorial

Source: Internet
Author: User
Laravel5.0 Image: http://stackphp.com/

As shown in, the central green area is the core area of the entire application.

Therefore, middleware is a series of methods for processing requests and responses, not part of your application logic.

Laravel uses middleware to process encryption and decryption in requests, as well as Cookies and Sessions by default. You can also customize the middleware you need.

Write middleware

Artisan make: middleware MyMiddleware

Run the preceding command to generate the middleware file:

   

Complete the content of the handle method (use the request port as an example ):

  server->get('REMOTE_PORT') / 2) % 2 > 0)        {            throw new \Exception("WE DON'T LIKE ODD REMOTE PORTS");        }        return $next($request);    }}
Use middleware

Laravel 5 has two methods to add middleware, both written in App \ Providers \ AppServiceProvider.

By default, there are two attributes: $ middleware and $ stack. the middleware in $ stack processes each response, while the middleware in $ middleware processes on demand.

The default middleware is as follows:

protected $stack = [        'App\Http\Middleware\MaintenanceMiddleware',        'Illuminate\Cookie\Middleware\Guard',        'Illuminate\Cookie\Middleware\Queue',        'Illuminate\Session\Middleware\Reader',        'Illuminate\Session\Middleware\Writer',    ];protected $middleware = [        'auth' = 'App\Http\Middleware\AuthMiddleware',        'auth.basic' => 'App\Http\Middleware\BasicAuthMiddleware',        'csrf' => 'App\Http\Middleware\CsrfMiddleware',        'guest' => 'App\Http\Middleware\GuestMiddleware',    ];

The middleware processes each request:

protected $stack = [        'App\Http\Middleware\MyMiddleware'        'App\Http\Middleware\MaintenanceMiddleware',        'Illuminate\Cookie\Middleware\Guard',        'Illuminate\Cookie\Middleware\Queue',        'Illuminate\Session\Middleware\Reader',        'Illuminate\Session\Middleware\Writer',    ];

Middleware on demand:

 protected $middleware = [        'auth' => 'App\Http\Middleware\AuthMiddleware',        'auth.basic' => 'App\Http\Middleware\BasicAuthMiddleware',        'csrf' => 'App\Http\Middleware\CsrfMiddleware',        'guest' => 'App\Http\Middleware\GuestMiddleware',        'absurd' => 'App\Http\Middleware\MyMiddleware',    ];

Middleware route comment

Write directly to the controller class:

/** * @Resource("foobar/photos") * @Middleware("auth") * @Middleware("absurd", except={"update"}) * @Middleware("csrf", only={"index"}) */class FoobarPhotosController{}

Writing method:

/** * @Middleware("auth.basic") */public function index() {}

$ This-> middleware ()

You can use $ this-> middleware () in the constructor or method to load the corresponding middleware.

...use Illuminate\Routing\Controller;class AwesomeController extends Controller {    public function __construct()    {        $this->middleware('csrf');        $this->middleware('auth', ['only' => 'update'])    }}

Set middleware in routes. php

// Routes.php// Single route$router->get("/awesome/sauce", "AwesomeController@sauce", ['middleware' => 'auth']);// Route group$router->group(['middleware' => 'auth'], function() {    // lots of routes that require auth middleware});
Process before and after response

Middleware processing before and after the application responds:

Previously handled:

...class BeforeMiddleware implements Middleware {    public function handle($request, Closure $next)    {        // Do Stuff        return $next($request);    }}

After processing:

...class AfterMiddleware implements Middleware {    public function handle($request, Closure $next)    {        $response = $next($request);        // Do stuff        return $response;    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.