How to Use subdomain in Routes in laravel

Numerous cutting edge web applications offer redid substance to their clients, including giving them a custom subdomain where they can get to their substance. For instance, rather than a client’s profile page being http://example.com/clients/37, we might need to offer . By transforming some DNS and Apache settings, we can without much of a stretch give the same usefulness in Laravel.

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:

  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. <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 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');
        }
      }

     

    • 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 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' => '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 follows

     

    In the command line, run the migration as follows:

    php artisan migrate
    
    
    • 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 .

     

    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);
    
        });
    });

     

Recommended :  How to schedule commands in laravel 5.1

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest

Shares