我本來是在主題里面的api新建了個中間件,然后中間件里的參數不能傳遞給控制器,后來才發現多應用里的容器不能執行,換成app里的就可以,才發現多應用里的容器think\Request的問題,缺少了個斜杠,切記不能復制粘貼啊~~~
<?php #在app\api里面下穿件provider.php #api就是你多主題的文件然后不要復制app里面的弄成一模一樣,這樣就不會執行里面的request應用請求 #多應用引入需要寫成斜桿(\), use app\api\ExceptionHandle; use app\api\Request; // 容器Provider定義文件 return [ '\think\Request' => Request::class, '\think\exception\Handle' => ExceptionHandle::class, ];
<?php //middleware中間件代碼 declare(strict_types = 1); namespace app\api\middleware; use think\Response; // 判斷接口是否傳遞參數過來 class Auth { public function handle($request,\Closure $next){ if($request->param("name")){ $request->cate_name = $request->param("name"); } $response = $next($request); return $response; } }
<?php 控制器里 public function index(){ request()->cate_name //輸出name值 }