Vahn Marty
Vahn Marty

Reputation: 1478

How to create subdomain in Laravel dynamically?

In my Windows/System32/drivers/etc/hosts, I have this:

127.0.0.1   localhost
127.0.0.1   site.dev
127.0.0.1   *.site.dev

In my xampp/apache/conf/extra/httpd-vhost, I have this:

<VirtualHost site.dev>  
  DocumentRoot "C:/xampp_7/htdocs/"
  <Directory "C:/xampp_7/htdocs/">
  </Directory>
</VirtualHost>
<VirtualHost *.site.dev>  
  DocumentRoot "C:/xampp_7/htdocs/"
  <Directory "C:/xampp_7/htdocs/">
  </Directory>
</VirtualHost>

Now if I am going to run http://site.dev/project/public, It is working. I have this route command:

Route::group(['domain' => '{subdomain}.site.dev'], function($subdomain) {
    return $subdomain;
});

If I open http://sub.site.dev/startscript/public/ , I get an error of "This site can’t be reached".

The function of the program is that it can create subdirectories. Example, I have a business website. I can access/create like this.

inventory.mybusiness.com
sales.mybusiness.com
ad.mybusiness.com

Upvotes: 16

Views: 24594

Answers (3)

BlackPearl
BlackPearl

Reputation: 2775

If you are planning to develop a multi-tenancy app, you can use the TenancyForLaravel library. It is easy to use and takes care of routes, tenant database etc automatically.

You can find it at https://github.com/stancl/tenancy

Upvotes: 1

Parveen Chauhan
Parveen Chauhan

Reputation: 1496

I am using laravel version above 5

$appRoutes = function() {
    Route::get('/',function(){
        return view('welcome');
    }); 
};

Route::group(['subdomain' => '{subdomain}.yoursitename.com'], $appRoutes );

Place this code in your route file.
But in my case, the laravel app was placed after the root so is used subdomain Route like this

   $appRoutes = function() {
        Route::get('/foldername/',function(){
            return view('welcome');
        }); 
    };

 Route::group(['subdomain' => '{subdomain}.yoursitename.com/foldername'], $appRoutes );

Upvotes: 1

Vahn Marty
Vahn Marty

Reputation: 1478

I have solved it. I used Acyrlic DNS Proxy from this answer. Checkout the below link you will find the answer.

https://laracasts.com/discuss/channels/general-discussion/dynamic-sub-domain-creation-on-new-user-registration-in-laravel-5-and-wampserver

then the

Route::group(['domain' => '{account}.dns.dev'], function () {
    Route::get('/', function ($account) {
        return $account;
    });
});

is now working.

Upvotes: 21

Related Questions