I have a main controller CmsController, wich is extended to default laravel controller:
use App\Http\Controllers\Controller;
class CmsController extends Controller
{
protected $web = null;
public function __construct(Request $request)
{
$this->web = Web::domain($request->domain)->first();
}
}
Now, in this controller I want to call $this->web
use App\Http\Controllers\Web\PageController;
class PageController extends CmsController
{
public function getPage(Request $request)
{
dd($this->web); // returns always null
}
}
The data that should be returned is 100% correct, reqest params are also there...
Can someone give me a idea, what I did wrong here?
I think you need to execute parent constructor:
class PageController extends CmsController
{
public function __construct()
{
parent::__construct();
....
}
}
Because you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet, and even if you did the dd($this->web);
in the CmsController construct you will get null So you can do it like this :
use App\Http\Controllers\Controller;
class CmsController extends Controller
{
protected $web = null;
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->web = Web::domain($request->domain)->first();
return $next($request);
});
}
}
You are extending CmsController
and using PageController
thats why ,
use App\Http\Controllers\Web\CmsController;
class PageController extends CmsController
{
public function getPage(Request $request)
{
dd($this->web); // returns always null
}
}
I have changed,
use App\Http\Controllers\Web\PageController;
to this,
use App\Http\Controllers\Web\CmsController;