Laravel 子域路由不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41703192/
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
Laravel subdomain routing is not working
提问by 01000110
I'm trying to have an admin subdomain (like this)
我正在尝试拥有一个管理子域(像这样)
Route::group(['domain' => 'admin.localhost'], function () {
Route::get('/', function () {
return view('welcome');
});
});
but admin.localhostacts just like localhost. How I'm supposed to do this correctly?
但admin.localhost 的行为就像localhost。我应该如何正确地做到这一点?
I'm using Laravel 5.1 and MAMP on OSX
我在 OSX 上使用 Laravel 5.1 和 MAMP
回答by BrokenBinary
Laravel processes routes on a first-come-first-serve basis and therefore you need to place your least specific routes last in the routes file. This means that you need to place your route group above any other routes that have the same path.
Laravel 以先到先得的方式处理路由,因此您需要将最不具体的路由放在路由文件的最后。这意味着您需要将您的路由组放置在具有相同路径的任何其他路由之上。
For example, this will work as expected:
例如,这将按预期工作:
Route::group(['domain' => 'admin.localhost'], function () {
Route::get('/', function () {
return "This will respond to requests for 'admin.localhost/'";
});
});
Route::get('/', function () {
return "This will respond to all other '/' requests.";
});
But this example will not:
但是这个例子不会:
Route::get('/', function () {
return "This will respond to all '/' requests before the route group gets processed.";
});
Route::group(['domain' => 'admin.localhost'], function () {
Route::get('/', function () {
return "This will never be called";
});
});
回答by Matt The Ninja
Laravel's example...
Laravel 的例子...
Route::group(['domain' => '{account}.myapp.com'], function () {
Route::get('user/{id}', function ($account, $id) {
//
});
});
Your code
你的代码
Route::group(['domain' => 'admin.localhost'], function () {
Route::get('/', function () {
return view('welcome');
});
});
If you look at the laravel example it gets the parameter $account
in the route, this way we can route according to this variable. This can then be applied to the group or any route's in it..
如果你看 laravel 的例子,它获取了$account
路由中的参数,这样我们就可以根据这个变量进行路由。然后可以将其应用于组或其中的任何路由。
That said, if it's not something driven by your database and you just want it with admin subdomain i would personally do this as a nginx config.
也就是说,如果它不是由您的数据库驱动的东西,而您只想要它与管理子域,我会亲自将其作为 nginx 配置来执行。
If you want to test nginx locally (easier) i personally recommended doing development with docker.
如果您想在本地(更容易)测试 nginx,我个人建议使用 docker 进行开发。
Hope this answers your question, if not let me know and ill try to answer for you.
希望这能回答你的问题,如果没有让我知道,我会尽力为你回答。