2016-03-02 96 views
1

大家好:)我laravel 5文件routes.php文件,並返回該錯誤JWD過濾器不工作laravel 5

「在6243 BadMethodCallException compiled.php行: 方法過濾器不存在」

,但我曾正確laravel 4.2

我的代碼是:

Route::filter('authMobile', function($route, $request) 
{ 
try{ 
    $token = JWTAuth::getToken(); 
    $user = JWTAuth::toUser($token); 
    $tokenStr = JWTAuth::getToken()->__toString(); 
    if ($user->token != $tokenStr){ 
    throw new Exception("Login Token don't match"); 
    } 
    Session::put('user',$user->id); 
}catch(Exception $e){ 
    return Response::json(array(
     'error' => true, 
     'message' => 'Invalid Session: '.$e->getMessage() 
    )); 
} 
}); 

感謝,認爲

回答

0

正如你可以在錯誤看到:

"BadMethodCallException compiled.php line in 6243: Method filter does not exist."

這是關於這部分:Route::filter

在Laravel 5 Middleware是處理代替過濾器它的首選方式。他們沒有完全消失:

Filters are not removed in Laravel 5. You can still bind and use your own custom filters using before and after .

source Upgrade Guide 4.2 > 5.0(滾動到 路由過濾器)在這裏解釋瞭如何保持它與Route的工作,但我無論如何都會建議遷移;

The following Laravel features have been deprecated and will be removed entirely with the release of Laravel 5.2 in December 2015:

  • Route filters have been deprecated in preference of middleware.

source Upgrade Guide 5.1.0(滾動到棄用

您可以通過以下步驟張貼在docs把你authMobile到中間件,但我會建議你從作曲家更新包過,並採取請查看jwt-auth Authentication docs,其中詳細介紹了information關於如何使用已包含的中間件在Laravel 5中運行它。

如果您正在使用的0.5.*jwt-auth你只是讓他們在你的<appname>/Http/Kernel.php

protected $routeMiddleware = [ 
    ... 
    'jwt.auth'  => \Tymon\JWTAuth\Middleware\GetUserFromToken::class, 
    'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class,   
]; 

然後你就可以將其Controller水平,例如:

public function __construct() 
{ 
    // Here we can say we want to jwt auth all resource functions except index and show. 
    $this->middleware('jwt.auth', ['except' => ['index','show']]); 
} 

或者在你的路線。

Route::group(['middleware' => ['before' => 'jwt.auth', 'after' => 'jwt.refresh']], function() { 
..etc 

代替將過濾器就像你會在Laravel 4.2

+0

非常感謝你:)解決錯誤的問候 – Lucia