laravel 5 - 'auth' middleware in lumen throwing error -


i testing out lumen first time. trying out auth middleware throws error. want know whether such middleware shipped lumen or need implement our own? routes file

$app->group(['middleware' => 'auth'], function ($app) {      $app->get('/', ['as' => 'api', 'uses' => 'apicontroller@index']); }); 

and error when trying access route

errorexception in manager.php line 137: call_user_func_array() expects parameter 1 valid callback, class 'illuminate\auth\guard' not have method 'handle' 

as see, auth in lumen container bound illuminate\support\manager\authmanager. so, yeah, have create own middleware. example case.

make own middleware in app/http/middleware

<?php  namespace app\http\middleware;  use closure; use illuminate\contracts\auth\guard;  class authenticate {     /**      * guard implementation.      *      * @var guard      */     protected $auth;      /**      * create new filter instance.      *      * @param  guard  $auth      * @return void      */     public function __construct(guard $auth)     {         $this->auth = $auth;     }      /**      * handle incoming request.      *      * @param  \illuminate\http\request  $request      * @param  \closure  $next      * @return mixed      */     public function handle($request, closure $next)     {         if ($this->auth->guest()) {             if ($request->ajax()) {                 return response('unauthorized.', 401);             } else {                 // lumen has no redirector::guest(), line put intended url session redirector::guest()                 app('session')->put('url.intended', app('url')->full());                 // set login url here                 return redirect()->to('auth/login', 302);             }         }          return $next($request);     } } 

after this, bind middleware in container. can in bootstrap/app.php. add these 2 lines before return.

/* |-------------------------------------------------------------------------- | load application routes |-------------------------------------------------------------------------- | | next include routes file can added | application. provide of urls application | can respond to, controllers may handle them. | */  $app->group(['namespace' => 'app\http\controllers'], function ($app) {     require __dir__.'/../app/http/routes.php'; });  $app->bind('app\http\middleware\authenticate', 'app\http\middleware\authenticate'); $app->alias('app\http\middleware\authenticate', 'middleware.auth'); 

now, instead of using auth in middleware, use middleware.auth:

$app->group(['middleware' => 'middleware.auth'], function ($app) {     $app->get('/', ['as' => 'api', 'uses' => 'apicontroller@index']); }); 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -