我正在构建一个子域指向用户的应用程序。如何在路径中获取地址的子域名部分?
Route::group(array('domain' => '{subdomain}.project.dev'), function() {
Route::get('foo', function($subdomain) {
// Here I can access $subdomain
});
// How can I get $subdomain here?
});
但是,我已经建立了一个混乱的解决方案:
Route::bind('subdomain', function($subdomain) {
// Use IoC to store the variable for use anywhere
App::bindIf('subdomain', function($app) use($subdomain) {
return $subdomain;
});
// We are technically replacing the subdomain-variable
// However, we don't need to change it
return $subdomain;
});
我想在路由之外使用变量的原因是基于该变量建立数据库连接。
答案 0 :(得分:9)
$subdomain
被注入到实际的Route
回调中,它在group
闭包内未定义,因为Request
尚未被解析。
我不明白为什么你需要在实际Route
回调之外的群组关闭但中提供它。
如果您想要的是在解析Request
后的任何地方都可以使用它,您可以设置一个后置过滤器来存储$subdomain
值:
Config::set('request.subdomain', Route::getCurrentRoute()->getParameter('subdomain'));
更新:应用组过滤器来设置数据库连接。
Route::filter('set_subdomain_db', function($route, $request)
{
//set your $db here based on $route or $request information.
//set it to a config for example, so it si available in all
//the routes this filter is applied to
Config::set('request.subdomain', 'subdomain_here');
Config::set('request.db', 'db_conn_here');
});
Route::group(array(
'domain' => '{subdomain}.project.dev',
'before' => 'set_subdomain_db'
), function() {
Route::get('foo', function() {
Config::get('request.subdomain');
});
Route::get('bar', function() {
Config::get('request.subdomain');
Config::get('request.db');
});
});
答案 1 :(得分:8)
Shortly after这个问题被问到,Laravel实现了一种新的方法,用于在路由代码中获取子域。它可以像这样使用:
Route::group(array('domain' => '{subdomain}.project.dev'), function() {
Route::get('foo', function($subdomain) {
// Here I can access $subdomain
});
$subdomain = Route::input('subdomain');
});
参见"访问路由参数值" in the docs
答案 2 :(得分:1)
试试这个,因为所有的方法都不适用于Laravel 5.4
-- drop operator if exists -> (anyarray, int);
-- drop function if exists array_by_index(anyarray, int);
-- drop function if exists reduce_dim(anyarray);
create or replace function reduce_dim(anyarray)
returns setof anyarray as
$function$
declare
s $1%type;
begin
if ($1 = '{}') or ($1 is null) then
return;
else
foreach s slice 1 in array $1 loop
return next s;
end loop;
return;
end if;
end;
$function$
language plpgsql immutable;
create function array_by_index(anyarray, int) returns anyarray
language sql
immutable
as $$
select case when count(*) = 1 then reduce_dim($1[$2:$2]) else array_agg(x) end
from reduce_dim($1[$2:$2]) x
$$;
create operator -> (procedure = array_by_index, leftarg = anyarray, rightarg = int);
select
array[[[1,1],[2,2]],[[3,3],[4,4]]]->2,
array[[1,2],[3,4],[5,6]]->3,
array[[1,2],[3,4],[5,6]]->3->1;
╔═══════════════╤══════════╤══════════╗
║ ?column? │ ?column? │ ?column? ║
╠═══════════════╪══════════╪══════════╣
║ {{3,3},{4,4}} │ {5,6} │ {5} ║
╚═══════════════╧══════════╧══════════╝
答案 3 :(得分:1)
使用宏的方式:
Request::macro('subdomain', function () {
return current(explode('.', $this->getHost()));
});
现在您可以使用它:
$request->subdomain();
答案 4 :(得分:0)
$route = Route::getCurrentRoute();
现在您应该可以访问路线的所有内容。即
$route = Route::getCurrentRoute();
$subdomain = $route->getParameter('subdomain');
答案 5 :(得分:-3)
你可以为laravel 5做这样的事情:
public function methodName(Request $request) {
$subDomain = $request->route()->parameter('subdomain');
}