Laravel collection Middleware

Source: Internet
Author: User
Problem

You want to add middleware to your application but don't know where to begin.

Solution

Create a simple middleware class.

Step 1-create the class
<?php namespace MyApp;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\HttpKernel\HttpKernelInterface;class Middleware implements HttpKernelInterface {  protected $app;  /**   * Constructor   */  public function __construct(HttpKernelInterface $app)  {    $this->app = $app;  }  /**   * Handle the request, return the response   *   * @implements HttpKernelInterface::handle   *   * @param  \Symfony\Component\HttpFoundation\Request  $request   * @param  int   $type   * @param  bool  $catch   * @return \Symfony\Component\HttpFoundation\Response   */  public function handle(Request $request,    $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)  {    // 1) Modify incoming request if needed    ...    // 2) Chain the app handler to get the response    $response = $this->app->handle($request, $type, $catch);    // 3) Modify the response if needed    ...    // 4) Return the response    return $response;  }}?>
Step 2-register the middleware class

You need to do this inregister()Method of a service provider.

App::middleware(‘MyApp\Middleware‘);
 

Alternatively you can install a simple package I created which allows you to register your middleware inapp/start/preboot.php. See laravel-hooks for details.

Discussion

The above class doesn' t do anything.

 

But it's a good skeleton to start with. obviusly, you'll need to change the namespace and classname to fit your application.

Then you may want to try logging something to make sure it works. You can updatehandle()Method of your class as specified below.

// In step #1) Modify incoming request if needed// Log to a file. Since app/start/global.php hasn‘t been hit// yet the Log facade isn‘t set to log to a file yet. So just// write directly to a file.$logfile = storage_path().‘/logs/laravel.log‘;error_log("Middleware entry\n", 3, $logfile);// In step #3) Modify reponse if needed// Log to a file. We‘re safe to use the Log facade now that// it should be set up in app/start/global.phpLog::info("Middleware exit");

Now you can examine yourapp/storage/logs/laravel.logFile to see that your middleware works.

Laravel collection Middleware

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.