用户名作为 Laravel 上的子域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14401468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Username as subdomain on laravel
提问by Ivanka Todorova
I've set up a wildcard subdomain *.domain.com & i'm using the following .htaccess:
我已经设置了一个通配符子域 *.domain.com 并且我正在使用以下 .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www\.
RewriteCond %{HTTP_HOST} (.*)\.domain\.com
RewriteRule .* index.php?username=%1 [L]
Everything works perfectly.
一切正常。
I want to implement this method in laravel. Mainly I want to have my user's profile displayed when you go to username.domain.com. Any ideas on achieving this?
我想在 Laravel 中实现这个方法。主要是我想在您访问 username.domain.com 时显示我的用户个人资料。关于实现这一目标的任何想法?
回答by Laurence
This is easy. Firstly - do NOT change the .htaccessfile from the default provided by Laravel. By default all requests to your domain will be routed to your index.phpfile which is exactly what we want.
这很简单。首先 - 不要更改.htaccessLaravel 提供的默认文件。默认情况下,对您域的所有请求都将路由到您的index.php文件,这正是我们想要的。
Then in your routes.phpfile just use a 'before' filter, which filters all requests to your application before anything else is done.
然后在您的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
}
});
edit: if you have a country extension (i.e. domain.com.au or domain.com.eu) then you will want to change the count($server) to check for 4, not 3
编辑:如果您有国家/地区扩展名(即 domain.com.au 或 domain.com.eu),那么您需要更改计数($server)以检查 4,而不是 3
回答by Martti Laine
回答by mavrck
While I can't say what the full solution would be in your case, I would start with the SERVER_NAME value from the request (PHP: $_SERVER['SERVER_NAME']) such as:
虽然我无法说出您的情况的完整解决方案是什么,但我将从请求中的 SERVER_NAME 值(PHP:$_SERVER['SERVER_NAME'])开始,例如:
$username = str_replace('.domain.com', '', Request::server('SERVER_NAME'));
Make sure you additionally clean/sanitize the username, and from there you can lookup the user from the username. Something like:
确保您另外清理/清理用户名,然后您可以从用户名中查找用户。就像是:
$user = User::where('username', '=', $username)->first();
Somewhere in the routes file you could conditionally define a route if the SERVER_NAME isn't www.domain.com, or domain.com, though I'm sure others can come up with a much more eloquent way for this part...
如果 SERVER_NAME 不是 www.domain.com 或 domain.com,您可以在路由文件中的某处有条件地定义路由,但我相信其他人可以为这部分提出更雄辩的方法......
回答by QArea
ability add subdomains like subdomain *. domain.com should be switched by your hosting provider, in the .htaccess you can not configure support of subdomains
能够添加子域,如子域 *. domain.com 应该由您的托管服务提供商切换,在 .htaccess 中您不能配置对子域的支持

