dtpyvb1873 2016-03-02 11:35
浏览 79
已采纳

jwd过滤器不起作用laravel 5

hello everyone :) in my laravel 5 file routes.php, and return this error

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

but I worked correctly laravel 4.2

My code is:

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()
    ));
}
}); 

Thanks, regards

  • 写回答

1条回答 默认 最新

  • dongzuoyue6556 2016-03-02 13:12
    关注

    As you can see in the error:

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

    This is about this part: Route::filter.

    In Laravel 5 Middleware is the preferred way to handle it, instead of filters. They are not entirely gone:

    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 (Scroll to Route Filters) Here is explained how you can keep it working with Route, but I would suggest migration anyway;

    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 (Scroll to Deprecations)

    You can turn your authMobile into middleware by following the steps as posted in the docs, but I would suggest you update your package from composer too and take a look at the jwt-auth Authentication docs, which has detailed information on how to make it work in Laravel 5 with the already included middleware.

    If you are using 0.5.* of jwt-auth you just enable them in your <appname>/Http/Kernel.php:

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

    Then you can include it in Controller level for example:

    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']]);
    }
    

    Or in your routes.

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

    Instead of applying the filters like you would in Laravel 4.2

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 武汉岩海低应变分析软件,导数据库里不显示波形图
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥30 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile