api - group middleware not working in laravel 5.4 -
i create restfull api using laravel 5.4 , create simple crud demo middleware group routing. route::group(['middleware' => 'auth:api'] it's not working.
my routes/api.php file following
use app\article; use illuminate\http\request; route::middleware('auth:api')->get('/user', function (request $request) { return $request->user(); }); route::post('register', 'auth\registercontroller@register'); route::post('login', 'auth\logincontroller@login'); route::post('logout', 'auth\logincontroller@logout'); route::group(['middleware' => 'auth:api'], function() { route::get('articles', 'articlecontroller@index'); route::get('articles/{article}', 'articlecontroller@show'); route::post('articles', 'articlecontroller@store'); route::put('articles/{article}', 'articlecontroller@update'); route::delete('articles/{article}', 'articlecontroller@delete'); });
and controller file app\http\controller\articlecontroller .php following namespace app\http\controllers;
use app\article; use illuminate\http\request; class articlecontroller extends controller { public function index() { return article::all(); } public function show(article $article) { return $article; } public function store(request $request) { $article = article::create($request->all()); return response()->json($article, 201); } public function update(request $request, article $article) { $article->update($request->all()); return response()->json($article, 200); } public function delete(article $article) { $article->delete(); return response()->json(null, 204); } }
but it's not working. please, give me full solution.
controller\autth\logincontroller.php
namespace app\http\controllers\auth; use app\http\controllers\controller; use illuminate\foundation\auth\authenticatesusers; use illuminate\http\request; use illuminate\support\facades\auth; class logincontroller extends controller { /* |-------------------------------------------------------------------------- | login controller |-------------------------------------------------------------------------- | | controller handles authenticating users application , | redirecting them home screen. controller uses trait | conveniently provide functionality applications. | */ use authenticatesusers; /** * redirect users after login. * * @var string */ protected $redirectto = '/home'; public function login(request $request) { $this->validatelogin($request); if ($this->attemptlogin($request)) { $user = $this->guard()->user(); $user->generatetoken(); return response()->json([ 'data' => $user->toarray(), ]); } return $this->sendfailedloginresponse($request); } public function logout(request $request) { $user = auth::guard('api')->user(); if ($user) { $user->api_token = null; $user->save(); } return response()->json(['data' => 'user logged out.'], 200); } /** * create new controller instance. * * @return void */ public function __construct() { $this->middleware('guest')->except('logout'); } }
and auth\registrocontroller.php
namespace app\http\controllers\auth; use app\http\controllers\controller; use app\user; use illuminate\foundation\auth\registersusers; use illuminate\http\request; use illuminate\support\facades\validator; class registercontroller extends controller { /* |-------------------------------------------------------------------------- | register controller |-------------------------------------------------------------------------- | | controller handles registration of new users | validation , creation. default controller uses trait | provide functionality without requiring additional code. | */ use registersusers; /** * redirect users after registration. * * @var string */ protected $redirectto = '/home'; /** * create new controller instance. * * @return void */ public function __construct() { $this->middleware('guest'); } /** * validator incoming registration request. * * @param array $data * @return \illuminate\contracts\validation\validator */ protected function validator(array $data) { return validator::make($data, [ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', 'password' => 'required|string|min:6|confirmed', ]); } protected function registered(request $request, $user) { $user->generatetoken(); return response()->json(['data' => $user->toarray()], 201); } /** * create new user instance after valid registration. * * @param array $data * @return \app\user */ protected function create(array $data) { return user::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); } }
and user.php model following:
namespace app; use app\user; use illuminate\http\requests; use illuminate\notifications\notifiable; use illuminate\foundation\auth\user authenticatable; // use zizaco\entrust\traits\entrustusertrait; use app\http\controllers\controller; class user extends authenticatable { use notifiable; //, entrustusertrait; /** * attributes mass assignable. * * @var array */ public function index() { return user::all(); } protected $fillable = [ 'name', 'email', 'password', ]; /** * attributes should hidden arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; public function generatetoken() { $this->api_token = str_random(60); $this->save(); return $this->api_token; } }
you using resource route. prefer simple code
route::resource('articles', 'articlecontroller');
and add middleware in controller
use app\article; use illuminate\http\request; class articlecontroller extends controller { public function __construct() { $this->middleware('auth:api'); } public function index() { return article::all(); } public function show(article $article) { return $article; } public function store(request $request) { $article = article::create($request->all()); return response()->json($article, 201); } public function update(request $request, article $article) { $article->update($request->all()); return response()->json($article, 200); } public function delete(article $article) { $article->delete(); return response()->json(null, 204); } }
Comments
Post a Comment