Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sheixt's avatar

Subdomain routing, setting global variables for controllers and views

I'm attempting to create a set of resourceful routes which are prefixed with a wildcard subdomain associated to an account/company.

The issue I have is when I use the route() helper it requires the account/company to be passed as a second parameter (which I'd rather not set in each view). Equally, I'd like to have the $current_company set globally so I can access it within each of the controllers for eloquent queries etc.

What is the best approach to set this variable once (perhaps on login by the authenticated() method?) and to have it globally accessible? I've considered a service provider, middleware but (as I'm relatively new to Laravel) I'm not sure on the best approach.

I'm using Laravel 5.4. This is my web.php routes file:

    
  Route::domain('{company}.appointments.localhost')->group(function () {
      // Home
      Route::get('/home', 'HomeController@index')->name('home');

      // Companies
      Route::resource('companies' , 'CompanyController');

      // Locations
      Route::resource('locations' , 'LocationController');

      // Services
      Route::resource('services' , 'ServiceController');

      // Bookings
      Route::resource('bookings' , 'BookingController');
  });

Happy to provide any additional details if it will aid answering the question. Thanks in advance!

0 likes
5 replies
Dry7's avatar

Check

Route::current()->parameter('company')

Perhaps a variable there is. This code can be called anywhere.

sheixt's avatar

@Dry7 thanks for the suggestion. Could you clarify where this would be placed? Just when I put this within the web.php I encounter a FatalThrowableError: Call to a member function parameter() on null exception.

Is this because the company parameter isn't set? Which comes to the other part of my question... where would be best to set the variable globally?

Dry7's avatar

No, it's because you connected another class Remove from use block class called Route and add

use Illuminate\Support\Facades\Route;

or just use

print_r(\Illuminate\Support\Facades\Route::current()->parameter('company'));
sheixt's avatar

Sorry @Dry7 I misunderstood you, I thought you meant to place that in the routes/web.php but after playing around I understand you were referring to the controller. Thanks!!

I can get to that variable within individual controllers (for example in the HomeController). But I would like to set this only once, rather than in each individual controller... in a parent controller or something (and then pass it down to the views via View::share()). What's the best way to do this?

sheixt's avatar

Just for some context... in my views I use theroute() helper, which requires the company variable be passed as a second parameter (eg. route('locations.index')).

I'd like to avoid having to pass this in every view if possible....

Please or to participate in this conversation.