laravel控制器使用中间件传递参数
标签:
laravel
大家好,好久没有更新文章了,猪哥哥在这里祝大家新年快乐!今天猪哥来说下如何在控制器中使用中间件传递参数,我们通过一系列的实验来验证下我们的想法到底对不对,来帮助那些还没有理解 中间件的用法的那些同学们,好了废话不多说了直接开始我们的实验
开始之前我们先看一个截图
这个是一个构造函数 之前猪哥刚学习Laravel 自定义中间件的时候 看到这个写法 就懵了,这加个冒号啥东东啊?。。刚开始还以为是使用了guest 中间件 和自己自定义的 admin中间件,稀里糊涂的就没怎么研究,后来随着猪哥技术有所提高 开始研究底层代码 才发现 这是 guest 使用的中间件 位置是在
App\Http\Middleware\RedirectIfAuthenticated
guest 中间件使用的就是 RedirectIfAuthenticated 这个类
这个类可以分别定义 前台守卫和后台守卫的相关逻辑 比如登录后该做什么
例如登录成功跳转 看代码
public function handle($request, Closure $next, $guard = null) { /*if (Auth::guard($guard)->check()) { return redirect(RouteServiceProvider::HOME); }*/ if (Auth::guard($guard)->check()) { $url = $guard == 'admin' ? 'admin/index' : '/home'; return redirect($url); } return $next($request); }
我们可以发现 在handle 这个方法里面 有个 $guard 这个参数 ,其实这个参数就是之前 截图里面 guest:admin 冒号后面的 admin
public function __construct() { $this->middleware('guest:admin'); } // guest是中间件的名称 :admin 是传递的参数默认 是定义的守卫的名字 在app\config\auth.php 中定义
如下面所示
<?php return [ /* |-------------------------------------------------------------------------- | Authentication Defaults |-------------------------------------------------------------------------- | | This option controls the default authentication "guard" and password | reset options for your application. You may change these defaults | as required, but they're a perfect start for most applications. | */ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], /* |-------------------------------------------------------------------------- | Authentication Guards |-------------------------------------------------------------------------- | | Next, you may define every authentication guard for your application. | Of course, a great default configuration has been defined for you | here which uses session storage and the Eloquent user provider. | | All authentication drivers have a user provider. This defines how the | users are actually retrieved out of your database or other storage | mechanisms used by this application to persist your user's data. | | Supported: "session", "token" | */ 'guards' => [ 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', 'hash' => false, ], ], /* |-------------------------------------------------------------------------- | User Providers |-------------------------------------------------------------------------- | | All authentication drivers have a user provider. This defines how the | users are actually retrieved out of your database or other storage | mechanisms used by this application to persist your user's data. | | If you have multiple user tables or models you may configure multiple | sources which represent each model / table. These sources may then | be assigned to any extra authentication guards you have defined. | | Supported: "database", "eloquent" | */ 'providers' => [ 'admins' => [ 'driver' => 'eloquent', 'model' => App\Model\Admin::class, ], 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], // 'users' => [ // 'driver' => 'database', // 'table' => 'users', // ], ], /* |-------------------------------------------------------------------------- | Resetting Passwords |-------------------------------------------------------------------------- | | You may specify multiple password reset configurations if you have more | than one user table or model in the application and you want to have | separate password reset settings based on the specific user types. | | The expire time is the number of minutes that the reset token should be | considered valid. This security feature keeps tokens short-lived so | they have less time to be guessed. You may change this as needed. | */ 'passwords' => [ 'users' => [ 'provider' => 'users', 'table' => 'password_resets', 'expire' => 60, 'throttle' => 60, ], ], /* |-------------------------------------------------------------------------- | Password Confirmation Timeout |-------------------------------------------------------------------------- | | Here you may define the amount of seconds before a password confirmation | times out and the user is prompted to re-enter their password via the | confirmation screen. By default, the timeout lasts for three hours. | */ 'password_timeout' => 10800, ];
下面我们通过实验验证下到底对不对
首先我们在
App\Http\Middleware\RedirectIfAuthenticated
这个guest 的中间件类加个断点看看能不能拿到我们传递过来的冒号的值
public function handle($request, Closure $next, $guard = null) { /*if (Auth::guard($guard)->check()) { return redirect(RouteServiceProvider::HOME); }*/ dd($guard); if (Auth::guard($guard)->check()) { $url = $guard == 'admin' ? 'admin/index' : '/home'; return redirect($url); } return $next($request); }
如图所示:
然后我们在 登录页面的构造函数里面修改下 下图
我们访问login 方法在浏览器中看看结果
看到没有这里果然输出了 admin 由此我们就知道了 在 使用中间件 时候可以传递中间件的名字后面可以加冒号 传递参数
guest是中间件的名称 :admin 是传递的参数默认 是定义的守卫的名字 在app\config\auth.php 中定义
想必看到这里各位同学 是不是对使用中间件 有了更深入的了解,感谢大家的阅读,猪哥祝大家2020年事业有成,发大财!