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 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败