简体   繁体   English

如何在 Laravel 中动态创建子域?

[英]How to create subdomain in Laravel dynamically?

In my Windows/System32/drivers/etc/ hosts , I have this:在我的 Windows/System32/drivers/etc/ hosts 中,我有这个:

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:在我的 xampp/apache/conf/extra/ httpd-vhost 中,我有这个:

<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.现在,如果我要运行http://site.dev/project/public ,它正在工作。 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".如果我打开http://sub.site.dev/startscript/public/ ,我会收到“无法访问此站点”的错误消息。

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

I have solved it.我已经解决了。 I used Acyrlic DNS Proxy from this answer.我从这个答案中使用了 Acyrlic DNS 代理。 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 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.现在正在工作。

I am using laravel version above 5我使用的是 5 以上的 Laravel 版本

$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但在我的情况下,laravel 应用程序放置在根之后,因此使用子域 Route 像这样

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

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

If you are planning to develop a multi-tenancy app, you can use the TenancyForLaravel library.如果您计划开发多租户应用程序,您可以使用TenancyForLaravel库。 It is easy to use and takes care of routes, tenant database etc automatically.它易于使用并自动处理路线、租户数据库等。

You can find it at https://github.com/stancl/tenancy你可以在https://github.com/stancl/tenancy找到它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM