Laravel Subdomain - localhost

Shah Md. Iktakhairul Islam
1 min readOct 24, 2021

--

In your database user table column name domain is for each sub domain. Example —

'name'     => 'Shah Md. Iktakhairul Islam',
'email' => 'iktakhairul@gmail.com',
'password' => Hash::make('admin'),
'domain' => 'developer',
'role' => 'developer',
'weight' => 59.00,
  1. .env
APP_URL=http://laravel-sub-domain.com
SUB_URL=laravel-sub-domain.com

2. config/app.php create line bellow url.

'short_url' => env('SUB_URL', 'http://localhost'),

3. config/session.php

'domain' => env('SESSION_DOMAIN', '.laravel-sub-domain.com'),

4. route/web.php

Route::domain('{subdomain}.'.config('app.short_url'))->group(function () {    Route::resource('users', UserController::class);
Route::resource('products', ProductController::class);
});

5. Add user and profile resource with view. Each function must have to pass subdomain like-

public function index($subdomain)
{
$users = DB::table('users')->get();

return view("users.index", compact('users', 'subdomain'));
}
public function create($subdomain)
{
return view("users.create", compact('subdomain'));
}

6. Configure your Local Hosts,

Ubuntu command — sudo vim /etc/hosts

edit like user table domain —

127.0.0.1 localhost127.0.0.1 laravel-sub-domain.com127.0.0.1 system.laravel-sub-domain.com
127.0.0.1 developer.laravel-sub-domain.com

7. Hope this is ok.

Route — laravel-sub-domain.com/users

and enjoy subdomain in localhost.

8. If you have login system, then to set logout function in your Auth Login Controller like—

public function logout(Request $request)
{
$this->guard()->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();

return redirect()->to('http://laravel-sub-domain.com:8000/');

— Shah Md. Iktakhirul Islam, Software Engineer.

Contact Number: 01683201359

Email: iktakhairul@gmail.com

GitHub Profile: https://github.com/iktakhairul

--

--