Laravel Framework realizes the operation logging function using middleware

Source: Internet
Author: User
This article mainly introduces the Laravel framework realizes the function of operation logging using middleware, analyzes the creation and introduction of Laravel framework middleware and the related implementation techniques of using middleware to operate logging function, and the need of friends can refer to the following

In this paper, the Laravel framework is used to realize the operation logging function using middleware. Share to everyone for your reference, as follows:

The process of logging operations using middleware:

1. Create middleware

PHP Artisan Make:middleware Adminoperationlog

2. Generated file ./app/http/middleware/adminoperationlog.php

The code is as follows:

<?phpnamespace app\http\middleware;use closure;use illuminate\http\request;use Illuminate\Support\Facades\Auth; Use App\http\models\operationlog;class adminoperationlog{  /**   * Handle an incoming request.   *   * @param \illuminate\http\request $request   * @param \closure $next   * @return Mixed   * * Public  function handle ($request, Closure $next)  {    $user _id = 0;    if (Auth::check ()) {      $user _id = (int) auth::id ();    }    $_server[' admin_uid '] = $user _id;    if (' GET '! = $request->method ()) {      $input = $request->all ();      $log = new Operationlog (); # Create a table in advance, model      $log->uid = $user _id;      $log->path = $request->path ();      $log->method = $request->method ();      $log->ip = $request->ip ();      $log->sql = ";      $log->input = Json_encode ($input, json_unescaped_unicode);      $log->save ();  # log    }    return $next ($request);}  }

3. Middleware Introduction ./app/http/kernel.php

protected $middlewareGroups = [    ' web ' = [      ...      \app\http\middleware\adminoperationlog::class,      ...    ],    ' api ' = = [      ' throttle:60,1 ',      ' Bindings ',    ],  ];

The operation log is logged at this time

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.