鍍金池/ 問(wèn)答/PHP/ laravel設(shè)置中間件后打開(kāi)路由報(bào)錯(cuò)是怎么回事?

laravel設(shè)置中間件后打開(kāi)路由報(bào)錯(cuò)是怎么回事?

原來(lái)的路由是這樣的Route::get('/setting/myinfo','HomeController@myinfo');
加上中間件后是這樣的Route::get('/setting/myinfo',['middleware'=>'login','HomeController@myinfo']);
Kernel.php里面這樣寫(xiě)的'login'=> \App\Http\Middleware\LoginMiddleware::class,
LoginMiddleware里面這樣寫(xiě)的

<?php

namespace App\Http\Middleware;

use Closure;

class LoginMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {   
        if(!request()->cookie('uid')){
            return redirect('/'); //沒(méi)有就跳至首頁(yè)
        }
        return $next($request);
    }
}

然后訪問(wèn)這個(gè)路由的時(shí)候報(bào)錯(cuò)了
圖片描述

回答
編輯回答
蝶戀花

Route::get('/setting/myinfo',['middleware'=>'login','HomeController@myinfo']);
改為:
Route::get('/setting/myinfo','HomeController@myinfo')->middleware('login');

以下是 5.5 創(chuàng)建路由的源碼:

protected function createRoute($methods, $uri, $action)
    {
        // If the route is routing to a controller we will parse the route action into
        // an acceptable array format before registering it and creating this route
        // instance itself. We need to build the Closure that will call this out.
        if ($this->actionReferencesController($action)) {
            $action = $this->convertToControllerAction($action);
        }

        $route = $this->newRoute(
            $methods, $this->prefix($uri), $action
        );

        // If we have groups that need to be merged, we will merge them now after this
        // route has already been created and is ready to go. After we're done with
        // the merge we will be ready to return the route back out to the caller.
        if ($this->hasGroupStack()) {
            $this->mergeGroupAttributesIntoRoute($route);
        }

        $this->addWhereClausesToRoute($route);

        return $route;
    }

以后貼報(bào)錯(cuò)圖的時(shí)候,關(guān)鍵的堆棧信息要貼出來(lái)的,不然很難幫助到你。

2017年6月24日 02:37