Iva*_*ova 9 php .htaccess mod-rewrite laravel laravel-3
我已经设置了一个通配符子域*.domain.com&我正在使用以下.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www\.
RewriteCond %{HTTP_HOST} (.*)\.domain\.com
RewriteRule .* index.php?username=%1 [L]
Run Code Online (Sandbox Code Playgroud)
一切都很完美.
我想在laravel中实现这个方法.主要是我想在访问username.domain.com时显示我的用户个人资料.实现这一目标的任何想法?
Lau*_*nce 23
这很简单.首先 - 不要更改.htaccessLaravel提供的默认文件.默认情况下,对您域名的所有请求都将路由到您的index.php文件,这正是我们想要的.
然后在您的routes.php文件中使用"之前"过滤器,该过滤器会在完成任何其他操作之前过滤对您的应用程序的所有请求.
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
}
});
Run Code Online (Sandbox Code Playgroud)
编辑:如果您有国家/地区扩展名(即domain.com.au或domain.com.eu),那么您将需要更改计数($ server)以检查4,而不是3
Mar*_*ine 13
Laravel 4具有开箱即用的功能:
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('user/{id}', function($account, $id) {
// ...
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8185 次 |
| 最近记录: |