每当调用函数时自动创建日志(将数据插入 table)

Auto create logs (inserts data to table) whenever a function was called

我是 laravel 和 php 的新手。我的 主要目标 是在每次调用函数时调用 createLogs(),而不是将调用方法放在每个函数中,因为这很麻烦。我需要帮助。

我制作了一个名为 WebLogs 的控制器,其中包含一个名为 createLogs() 的函数,可将数据插入 table。我希望在调用另一个函数时自动调用它。我尝试使用 this solution 并将其放在 Controller class 中,因为 WebLogs 扩展了 Controller class,而我所有其他控制器都扩展了 Controller class,但解决方案似乎不起作用。

所以我的 Controller class 现在看起来像这样:

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    // Added this function from the solution I mentioned
    public function __call($method, $arguments) {
        echo 'hello world';
        echo '<br><br>';
        echo $method;

        if(method_exists($this, $method)) {
            return call_user_func_array(array($this,$method),$arguments);
        }
     }
 }

示例控制器:

class DashboardController extends Controller
{
    public function index()
    {
        (new WebLogs)->createLogs(); //I don't want to call this for every function
        return view('dashboard');
    }
    public function showSomething()
    {
        (new WebLogs)->createLogs();
        return view('something');
    }
    public function updateSomething()
    {
        (new WebLogs)->createLogs();
        return redirect()->back()->with('message','yeey');
    }
}

DashboardController 中的函数如何从 web.php 中调用:

Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
Route::get('/something', [DashboardController::class, 'showSomething'])->name('something');
Route::post('/something/update', [DashboardController::class, 'updateSomething'])->name('something.update');

创建 app\Classes\WebLogs.php 内容:

<?php
namespace App\Classes;

class WebLogs {
    public function __construct() {
      return "WebLogs class with construct function was initialized.";
    }
    public function createLogs($routeName,$routePath) {
      $status = 0;
      logger('WebLogs class is running:');
      logger([$routeName,$routePath]);
      // Save to database here
      // ...
      return $status;
    }
}

然后,创建一个AutoCreateLogs中间件,它会保存为app\Http\Middleware\AutoCreateLogs.php:

$ php artisan make:middleware AutoCreateLogs

含内容:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Classes\WebLogs;

class AutoCreateLogs
{
    public function handle(Request $request, Closure $next)
    {
        $route = Route::current();
        $routePath = $route->uri;
        $routeName = $route->action['as'];
        $w = new WebLogs;
        $w->createLogs($routeName,$routePath);
        return $next($request);
    }
}

并像这样使用这个中间件:

Route::middleware([AutoCreateLogs::class])->group(function () {
  Route::get('/', [App\Http\Controllers\DashboardController::class, 'index'])->name('dashboard');
  Route::get('/something', [App\Http\Controllers\DashboardController::class, 'showSomething'])->name('something');
  Route::post('/something/update', [App\Http\Controllers\DashboardController::class, 'updateSomething'])->name('something.update');
  Route::get('/something/{value}', [App\Http\Controllers\DashboardController::class, 'getSomething'])->name('get.something');
});

app\Http\Controllers\DashboardController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DashboardController extends Controller
{
    public function index()
    {
        $page = 'index';
        return view('welcome', ['page'=>$page]);
    }

    public function showSomething()
    {
        $page = 'showSomething';
        return view('welcome', ['page'=>$page]);
    }

    public function updateSomething()
    {
        $page = 'updateSomething';
        return response()->json(['page'=>$page]);
    }

    public function getSomething(Request $request)
    {
        $page = 'getSomething';
        return view('welcome', ['page'=>$page]);
    }
}

然后,清空 storage\logs\laravel.log,并使用示例路线 运行 http://laravel-me.com/something/value99 它将显示成功的结果:

[2022-02-18 22:56:09] local.DEBUG: WebLogs class is running:  
[2022-02-18 22:56:09] local.DEBUG: array (
  0 => 'get.something',
  1 => 'something/{value}',
)  

阅读更多关于中间件的信息:https://laravel.com/docs/8.x/middleware