zoukankan      html  css  js  c++  java
  • laravel 拾遗 中间件

    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 SymfonyComponentHttpFoundationRequest;
    use SymfonyComponentHttpFoundationResponse;
    use SymfonyComponentHttpKernelHttpKernelInterface;
    
    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  SymfonyComponentHttpFoundationRequest  $request
       * @param  int   $type
       * @param  bool  $catch
       * @return SymfonyComponentHttpFoundationResponse
       */
      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 in the register() method of a service provider.

    App::middleware('MyAppMiddleware');
     

    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. Obviously, 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 update the handle() 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
    ", 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.php
    Log::info("Middleware exit");

    Now you can examine your app/storage/logs/laravel.log file to see that your middleware works.

  • 相关阅读:
    python 成功解决import librosa出错问题
    音频属性详解(入门解读)
    如何用python将txt中的package批量安装
    python生成一个WAV文件的正弦波
    图像处理方法(膨胀腐蚀,霍夫变换,滤波,去噪,图像增强,二值化,图片旋转,画直线)
    ORACLE数据库学习笔记1
    SICP:构造数据抽象--数据结构中队列与树的解释
    SICP:构造过程抽象--面向对象的解释
    Java学习笔记--文件IO
    Java学习笔记--异常机制
  • 原文地址:https://www.cnblogs.com/zhepama/p/3978152.html
Copyright © 2011-2022 走看看