#Adding Subdomains to RouteServiceProvider

13 messages · Page 1 of 1 (latest)

deft locust
#

Hello!

I'm trying to keep my routes files as clean as possible, so I'm trying to register different route files in RouteServiceProvider.php.

Basically, I'm separating them by subdomains, and then I'll slim down from there by requiring other route files.

Currently, here's what I have:

public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {

       Route::domain('admin.example.test')
                ->namespace($this->namespace)
                ->name('admin')
                ->as('admin.')
                ->middleware(['auth','can:access_admin', 'password.confirm'])
                ->group(base_path('routes/admin.php'));

        Route::middleware('web')
                ->namespace('App\Http\Controllers')
                ->group(base_path('routes/web.php'));
        });

Now when I run that, everything works as normal. Even when I run php artisan route:list, it shows that the routes are all there. Except when I go to the index page for the admin subdomain (admin.example.test), it redirects back to the dashboard on the primary domain (example.com/dashboard).

Is it not possible to add subdomains this way or am I doing something wrong?

proper cairn
#

Could also put all the subdomain routes in a different php file and register it

deft locust
deft locust
#

My idea is rather than declaring everything in the RouteServiceProvider, I can just make a routes file called subdomains.php and then list the subdomains in there. I’ll try that when this storm passes and let you know if it works.

deft locust
#

In RouteServiceProvider.php

Route::middleware('web')
  ->namespace($this->namespace)
  ->group(base_path('routes/subdomains.php'));
#

Then in routes/subdomains.php

$tld = config('fortify.domain');

Route::domain('admin.' . $tld)->name('admin')->as('admin.')->middleware(['auth', 'can:access_admin', 'password_confirm'])->group(function () {
  require(base_path('routes/subdomains/admin.php'));
});

#

Then in routes/subdomains/admin.php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UsersController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\AbilityController;
use App\Http\Controllers\AdminController;

// Main admin index
Route::controller(AdminController::class)->group(function() {
    Route::get('/', 'index')->name('index');
});

// User Controller
Route::resource('users', UsersController::class, ['middleware' => ['can:view_users']]);

// Roles Controller
Route::resource('roles', RoleController::class, ['middleware' => ['can:view_roles']]);

// Abilities Controller
Route::resource('abilities', AbilityController::class, ['middleware' => ['can:view_abilities']]);
#

Another thanks to @proper cairn for the help!