Using a subdomain in your route of laravel

Many modern web applications offer customized content to their users, including giving
them a custom subdomain where they can access their content. For example, instead of
a user’s profile page being http://example.com/users/37, we might want to offer
http://username.example.com. By changing some DNS and Apache settings, we
can easily provide the same functionality in Laravel.

Getting ready

For this recipe, we need access to our DNS settings and our server’s Apache configurations.
We’ll also need a properly configured MySQL database and a standard Laravel installation.
Throughout the recipe, we’ll be using example.com as the domain name.

How to do it..

To complete this recipe, follow these steps:
1. In the DNS for our domain name, we need to add an “A” record using a wildcard for the
subdomain, such as *.example.com, and then point it to our server’s IP address.
2. Open up Apache’s httpd.conf file and add a virtual host to it as follows:
<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
</VirtualHost>
3. In the command line, go to our application route and create a migration for our
names table:
php artisan migrate:make create_names_table
4. In the migrations directory, open the create_names_table file and add
our schema:
<?php
use Illuminate\Database\Migrations\Migration;
class CreateNamesTable extends Migration {
/**

* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(‘users’, function($table)
{
$table->increments(‘id’);
$table->string(‘name’);
$table->string(‘full_name’);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop(‘name’);
}
}

5. Back in the command line, create another migration to add some test data:
php artisan migrate:make add_names_data
6. Open the add_names_data file in the migrations directory:
<?php
use Illuminate\Database\Migrations\Migration;
class AddNamesData extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$names = array(

array(
‘name’ => ‘bob’,
‘full_name’ => ‘Bob Smith’
),
array(
‘name’ => ‘carol’,
‘full_name’ => ‘Carol Smith’
),
array(
‘name’ => ‘ted’,
‘full_name’ => ‘Ted Jones’
)
);
DB::table(‘name’)->insert($names);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::table(‘name’)->delete();
}
}

7. In the command line, run the migration as follows:
php artisan migrate
8. Create a route to get information from the names table based on the subdomain:
Route::get(‘/’, function()
{
$url = parse_url(URL::all());
$host = explode(‘.’, $url[‘host’]);
$subdomain = $host[0];
$name = DB::table(‘name’)->where(‘name’,$subdomain)-
>get();
dd($name);
});
9. In the browser, visit our domain with a relevant subdomain such as
http://ted.example.com.

How it works…

To start off, we need to update our DNS and our server. In our DNS, we create a wildcard
subdomain and create a virtual host in our Apache configuration. This makes sure that any
subdomains used will go to our main application.
For our default route, we use the parse_url function of PHP to get the domain name,
explode it into an array, and use only the first element. We can then

Leave a Reply

Your email address will not be published. Required fields are marked *