How to pass subdomain name into controllers and views in Laravel efficiently -


i'm building new application in laravel 5.5 rely heavily on subdomain names.

i have set route group subdomain routing, i'll need subdomain name variable available every controller , every view.

i have custom view composer, pass session variables every view. how can make subdomain name available view composer? don't think can access subdomain through request object.

also, possible pass subdomain name every controller , methods without needing specify parameter in every method?

currently, routing looks like:

route::domain('{sub}.something.com')->group(function () { route::prefix('admin')->group(function ($sub) {     route::get('/', 'admincontroller@methodroute1')->name('admin.route1');     route::get('/route2', 'admincontroller@methodroute2')->name('admin.route2');     route::post('/', 'admincontroller@methodroute3')->name('admin.route3'); /snip 

but way, need define every controller method as

public function method($sub){} 

even worse, call every named route , pass $sub parameter every view this:

public function method($sub){     if(sentinel::check()){         return redirect()->route('admin.route1',$sub);     } else {         return view('admin.route2')->with('sub',$sub);     } } 

and in view:

{{ route('admin.route1',$sub) }} 

i understand if need pass $sub variable views , controllers, i'd prefer automatize using view composer:

public function __construct() {     $this->user = session('user');     $this->userdata = session('userdata');     $this->permissions = session('permissions'); }   public function compose(view $view) {     $view->with(['user' => $this->user, 'userdata' => $this->userdata, 'permissions' => $this->permissions]); } 

so how $sub view composer able define as:

$this->subdomain = $sub_from_somewhere 

or doing wrong? couldn't find more efficient way build app subdomain logic in it.

thank much.

you can use helper function request() gives access current request parameters. in case can access "sub" value in blade that:

{{request("sub")}} 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -