For this tutorial, 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 tutorial, we’ll be using example.com
as the domain name.
To complete this tutorial, follow these steps:
- 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. <VirtualHost *:80> ServerName example.com ServerAlias *.example.com </VirtualHost>
In the command line, go to our application route and create a migration for our
names
table:php artisan migrate:make create_names_table
- In the
migrations
directory, open thecreate_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'); } }
- Back in the command line, create another migration to add some test data:
php artisan migrate:make add_names_data
- Open the
add_names_data
file in themigrations
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' => 'Deven', 'full_name' => 'Deven rathore' ) ); DB::table('name')->insert($names); } /** * Reverse the migrations. * * @return void */ public function down() { DB::table('name')->delete(); } }
Open up Apache’s
httpd.conf
file and add a virtual host to it as followsIn the command line, run the migration as follows:
php artisan migrate
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 query the database using the subdomain and create a customized experience for the user.This tutorial allows for a single route to process the subdomains, but if we would like to use more routes with a subdomain, we could use a route group similar to the following:
Route::group(array('domain' => '{subdomain}.myapp.com'), function() { Route::get('/', function($subdomain) { $name = DB::table('name')->where('name', $subdomain)->get(); dd($name); }); });
- 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); });
- In the browser, visit our domain with a relevant subdomain such as .
- Create a route to get information from the
- Back in the command line, create another migration to add some test data: