dtpyvb1873 2016-03-02 11:35 采纳率: 0%
浏览 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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?