php - Extending Laravel base controller -
i newbie in laravel framework , want extend base controller in turn extends controller. however, discovered when that, controller no longer recognises session variables.
here code
namespace app\http\controllers\settings; use illuminate\http\request; use app\http\controllers\controller; use app\http\controllers\auth\permissioncontroller; use app\fee; class feecontroller extends permissioncontroller { /** * display listing of resource. * * @return \illuminate\http\response */ public function index(request $request) { dd(session('userdata')['user_urls']); $data['title']="fees"; $data['fees']=fee::all(); return view('settings.fee.index',$data); }
and permissioncontroller code
<?php namespace app\http\controllers\auth; use illuminate\support\facades\gate; use illuminate\http\request; use app\http\controllers\controller; class permissioncontroller extends controller { /** * create new controller instance. * * @return void */ public function __construct(request $request) { if(!session('userdata')['user_urls']->contains($request->path())){ dd(session('userdata')['user_urls']); } } }
but realize session('userdata')['user_urls']
becomes null @ permissioncontroller. if make feecontroller extend controller, session variables intact.
i need use session variables control @ permission controller.
i running laravel 5.3 on mac osx , php 7
i have figured out problem. actually, permissioncontroller not registered in web middleware group session not persisting in permissioncontroller. solution question make trait named permission instead of controller , use in feescontorller.
trait permission{ public function permission(request $request) { if($request->session()->get('name') != null){ echo "hello world"; } } }
and feescontroller this:
class feescontroller extends controller { use permission; public function index(request $request) { $this->permission($request); // method of trait. echo "\n".$request->session()->get('name'); } }
output:
if name attribute set in session :
hello world passion infinite
otherwise
no output // null
Comments
Post a Comment