Laravel5.3之Route,Middleware和Controller参数传递
博客专区 > botkenni 的博客 > 博客详情
Laravel5.3之Route,Middleware和Controller参数传递
botkenni 发表于1年前
Laravel5.3之Route,Middleware和Controller参数传递
  • 发表于 1年前
  • 阅读 29
  • 收藏 0
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

本文主要学习总结下Route,Middleware,Controller间参数传递。开发时经常碰到类似场景:有时需要在Middleware中读取Route中设置的middleware parameter和route parameter,有时也需要在Controller@Action中读取Middleware中设置的参数。

先假设路由是:

Route::group(['prefix' => 'route', 'namespace' => 'RouteParameter'], function () {

    // route/user/{user}/role/{role}, route parameter是{user}, {role}
    Route::group(['middleware' => 'route.parameter'], function () {
        Route::resource('user.role', 'RouteParameterToMiddlewareController');
    });

    // route/advisor, middleware parameter是 'advisor', 'show', 'comment'
    Route::group(['middleware' => 'middleware.parameter:advisor,show,comment'], function () {
        Route::resource('advisor', 'MiddlewareParameterToMiddlewareController');
    });
    
    // /route/controller, middleware parameter是 'client'
    Route::group(['middleware' => 'middleware.controller:client'], function () {
        Route::resource('controller', 'MiddlewareToController');
    });
});

路由对应的控制器是:

namespace App\Http\Controllers\RouteParameter;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RouteParameterToMiddlewareController extends Controller
{
    public function show($user, $role)
    {
        
    }
}


namespace App\Http\Controllers\RouteParameter;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class MiddlewareParameterToMiddlewareController extends Controller
{
    public function index()
    {
        return 'All';
    }
}

并且在Kernel.php中注册中间件:

$routeMiddlewares = []
    ...
    
    'route.parameter'       => \App\Http\Middleware\RouteParameter::class,
    'middleware.parameter'  => \App\Http\Middleware\MiddlewareParameter::class,
    'middleware.controller' => \App\Http\Middleware\ControllerParameter::class,
;

Passing Route Parameters to Middleware

在中间件route.parameter中读取路由参数有三种方式:$request->route($parameter_name);$request->route()->parameter($parameter_name);$request->route()->parameters(),代码如下:

<?php

namespace App\Http\Middleware;

use Closure;

class RouteParameter
{
    /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */
    public function handle($request, Closure $next)
    {
        // or $user = $request->route('user');
        $user = $request->route()->parameter('user'); // {user}
        $role = $request->route()->parameter('role'); // {role}
        dd($user, $role);
        $parameters = $request->route()->parameters(); // ['user' => {user}, 'role' => {role}]
        dd($parameters['user'], $parameters['role']);

        return $next($request);
    }
}

Passing Middleware Parameters to Middleware

在中间件middleware.parameter中读取中间件参数只需要在handle()中映射上对应的参数变量就行,就和在Controller@Action中获取路由参数一样:

namespace App\Http\Middleware;

use Auth;
use Closure;
use Illuminate\Http\Response;

class MiddlewareParameter
{
    /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param $role * @param $action * @param $data * * @return mixed */
    public function handle($request, Closure $next, $role, $action, $data)
    {
        dd($role, $action, $data); // 'advisor', 'show', 'comment'

        if (Auth::check() && (Auth::user()->type === $role)) {
            return $next($request);
        }

        return abort(Response::HTTP_UNAUTHORIZED, 'You must log in to access the resource.', [
            'Set-Cookie' => 'Laravel=0; path=/; Expires=Thu, 01-Jan-1970 00:00:00 GMT; Secure',
        ]);
    }
}

Passing Parameters in Middleware to Controller

有时需要在中间中把参数传递到控制器中,则可以通过Request对象作为传输管道,代码如:

namespace App\Http\Middleware;

use Closure;

class ControllerParameter
{
    /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */
    public function handle($request, Closure $next, $role)
    {
        $request->attributes->add(compact('role')); // 'client'
        return $next($request);
    }
}

控制器中使用Request对象获取$role参数:

namespace App\Http\Controllers\RouteParameter;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class MiddlewareToController extends Controller
{
    public function index(Request $request)
    {
        dd($request->get('role'));
    }
}

总结:Route,Middleware,Controller之间传递参数这个知识点会经常在设计代码时用到,还有从Route中传递参数到Controller中,这个就更是经常要用到的,也简单。总结下这几个知识点,便于查阅。

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 12
博文 278
码字总数 316624
×
botkenni
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: