【问题标题】:Laravel 8 Routing SubdomainLaravel 8 路由子域
【发布时间】:2021-05-23 14:34:04
【问题描述】:

我是 Laravel 8 的新手。在之前的 Laravel 版本 7 中,我们可以通过这种方式传递 子域 名称

    Route::group( [ 'domain' => '{admin}.example.com' ], function () {
        Route::get('/index', 'HomeController@index($account)' );
    }

但是,在 Laravel 8 中调用 Controller 的结构代码是这样改变的。

    Route::domain('{admin}.example.com')->group(function () {
        Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
    });

我一直在寻找文档,但没有找到。你能给我看文档或帮我发送/传递 subdomainweb.phpController

【问题讨论】:

  • 您的错误或问题是什么?
  • @matiaslauriti 我不知道使用 laravel 8 将我的子域名传递给我的控制器。在以前的版本中,我们可以使用 @index($admin)
  • 我不确定在以前的版本中 index($account) 是否有必要将参数传递给控制器​​。使用HomeController@index 并在控制器方法中使用参数一直有效

标签: php laravel subdomain


【解决方案1】:

您可以简单地在控制器方法中添加一个与路由参数同名的参数。 Laravel 负责在后台绑定变量。

文档中没有明确显示有关控制器的内容,但有一个基本示例。 https://laravel.com/docs/8.x/routing#route-group-subdomain-routing

下面是一个带有控制器的示例。

Route::domain('{subdomain}.example.com')->group(function () {
    Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
});
class HomeController 
{
    public function index($subdomain)
    {
        dd($subdomain);
    }
}

://admin.example.com/home

"admin"

【讨论】:

    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 2018-03-01
    • 2020-01-04
    • 2016-09-18
    • 2016-09-25
    • 2015-09-10
    • 2017-07-18
    • 2017-11-17
    • 2013-10-11
    相关资源
    最近更新 更多