[英]Laravel 5, Sub-Domain routing, with optional parameter
I've just started learning Laravel 5 and trying to create multilanguage web site and want to use different domains for the language so en.example.app points to English version, es.example.app to Spanish and so on. 我刚刚开始学习Laravel 5并尝试创建多语言网站,并希望为该语言使用不同的域名,因此en.example.app指向英语版本,es.example.app指向西班牙语等等。 I use route groups and below is my code.
我使用路由组,下面是我的代码。
Route::group(['domain' => '{domain}.example.app'], function() {
Route::get('/', function () {
return view('index');
});
Route::get('test', function(){
return view('index');
});
});
It works fine for all domains except example.app. 它适用于除example.app之外的所有域。 Unfortunately optional parameters {domain?} doesn't work for subdomains, and I don't want to duplicate routes like this.
不幸的是,可选参数{domain?}不适用于子域,我不想复制这样的路由。
Route::get('/', function () {
return view('index');
});
Route::get('test', function(){
return view('index');
});
Route::group(['domain' => '{domain}.example.app'], function() {
Route::get('/', function () {
return view('index');
});
Route::get('test', function(){
return view('index');
});
});
Could somebody please advise how to avoid this duplication? 有人可以建议如何避免这种重复吗?
You could create a file named app-routes.php
which contains all your routes and then in your actual routes.php
file 您可以创建一个名为
app-routes.php
的文件,其中包含您的所有路由,然后在您的实际routes.php
文件中
Route::group(['domain' => '{domain}.example.app'], function() {
include('app-routes.php');
});
Route::group(['domain' => 'example.app'], function() {
include('app-routes.php');
});
Thats becuase the {domain}.example.app
requires a .
多数民众赞成
{domain}.example.app
需要一个.
before example.app
. 在
example.app
之前。
You can remove the .
你可以删除
.
and add contraint for domain
parameter for it to have atmost 1 .
并为
domain
参数添加约束,使其至少为1 .
So the code will look like 所以代码看起来像
Route::group(['domain' => '{domain}example.app'], function($group) {
Route::get('/', function ($domain) {
//code
}) ;
// more routes
foreach($group->getRoutes() as $route){
$route->where('domain', '[a-z]+\.{0,1}');
}
});
PS : I don't know whether my regex is correct or not. PS:我不知道我的正则表达式是否正确。
A MiddleWare helped me. MiddleWare帮助了我。
Route::group(array('middleware' => 'resolve_domain'), function () {
Route::get('/', 'WhitePapersController@getHomepage');
});
And in MiddleWare - 在MiddleWare中 -
public function handle($request, Closure $next)
{
$params = explode('.', $request->getHost());
$sub_domains = config('admin_configs.editions'); // Predefined sub-domain
$edition = false;
if(!empty($params[0]) && in_array($params[0], $sub_domains, true)) {
$edition = $params[0];
}
define('DOMAIN_EDITION', $edition); // Set constant to be used.
return $next($request);
}
To use the domain parameter in your route function, you need to pass it through like so: 要在路由功能中使用domain参数,您需要像这样传递它:
Route::group(['domain' => '{domain}.example.app'], function() {
Route::get('/', function ($domain) {
// do something
});
});
It's covered in the docs here - http://laravel.com/docs/5.1/routing#route-group-sub-domain-routing 它在这里的文档中有所介绍 - http://laravel.com/docs/5.1/routing#route-group-sub-domain-routing
Your options are either the route duplication or a server level redirect for HTTP requests without a subdomain. 您的选项是路由重复或没有子域的HTTP请求的服务器级重定向。
The simple option is to just forward example.app
to www.example.app
. 简单的选择是将
example.app
转发到www.example.app
。
Route::group(['domain' => '{domain}.example.app'], function() {
});
Route::group(['domain' => 'example.app'], function() {
});
this pattern is good but if you want to use different language add localization file 这种模式很好,但如果你想使用不同的语言添加本地化文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.