php 域名后加用户名,php – 用户名作为laravel的子域名

这很容易首先 – 不要将.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值