php – 用户名作为laravel的子域名

php – 用户名作为laravel的子域名,第1张

概述我设置了一个通配符子域名* .domain.com&我正在使用以下.htaccess: Options +FollowSymLinksRewriteEngine OnRewriteBase /RewriteCond %{HTTP_HOST} !www\.RewriteCond %{HTTP_HOST} (.*)\.domain\.comRewriteRule .* index.php? 我设置了一个通配符子域名* .domain.com&我正在使用以下.htaccess:
Options +FollowSymlinksRewriteEngine OnRewriteBase /RewriteCond %{http_HOST} !www\.RewriteCond %{http_HOST} (.*)\.domain\.comRewriteRule .* index.PHP?username=%1 [L]

一切都很完美.

我想在laravel中实现这个方法.主要是想要在您访问username.domain.com时显示我的用户个人资料.有什么想法实现这个?

这很容易首先 – 不要将.htaccess文件从Laravel提供的默认文件中更改.默认情况下,您的域的所有请求将被路由到您的index.PHP文件,这正是我们想要的.

然后在您的routes.PHP文件中,只需使用’before’过滤器,在其他任何 *** 作完成之前,该过滤器将过滤对应用程序的所有请求.

Route::filter('before',function(){    // Check if we asked for a user    $server = explode('.',Request::server('http_HOST'));    if (count($server) == 3)     {        // We have 3 parts of the domain - therefore a subdomain was requested        // i.e.  user.domain.com        // Check if user is valID and has access - i.e. is logged in        if (Auth::user()->username === $server[0])        {            // User is logged in,and has access to this subdomain            // DO WHATEVER YOU WANT HERE WITH THE USER PROfile            echo "your username is ".$server[0];        }        else        {            // Username is invalID,or user does not have access to this subdomain            // SHOW ERROR OR WHATEVER YOU WANT            echo "error - you do not have access to here";        }    }    else    {        // Only 2 parts of domain was requested - therefore no subdomain was requested        // i.e. domain.com        // Do nothing here - will just route normally - but you Could put logic here if you want    }});

编辑:如果您有国家/地区扩展程序(即domain.com.au或domain.com.eu),那么您将要更改计数($server)以检查4,而不是3

总结

以上是内存溢出为你收集整理的php – 用户名作为laravel的子域名全部内容,希望文章能够帮你解决php – 用户名作为laravel的子域名所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1258431.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存