简体   繁体   English

Laravel子域路由不起作用

[英]Laravel subdomain routing is not working

I'm trying to have an admin subdomain ( like this ) 我正在尝试拥有一个管理子域名( 像这样

Route::group(['domain' => 'admin.localhost'], function () {
    Route::get('/', function () {
        return view('welcome');
    });
});

but admin.localhost acts just like localhost . 但是admin.localhost就像localhost一样。 How I'm supposed to do this correctly? 我该如何正确地做到这一点?

I'm using Laravel 5.1 and MAMP on OSX 我在OSX上使用Laravel 5.1和MAMP

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. Laravel以先来先服务的方式处理路线,因此您需要将最少的特定路线放在路线文件中。 This means that you need to place your route group above any other routes that have the same path. 这意味着您需要将路由组放在具有相同路径的任何其他路由之上。

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";
    });
});

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. 如果您查看laravel示例,它会在路径中获取参数$account ,这样我们就可以根据此变量进行路由。 This can then be applied to the group or any route's in it.. 然后可以将其应用于组或其中的任何路径。

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. 也就是说,如果它不是由您的数据库驱动的东西,而您只是希望它与admin子域,我个人会这样做作为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. 希望这能回答你的问题,如果不是让我知道并生病,试着为你回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM