【问题标题】:Laravel set parent route group parameters from child routeLaravel 从子路由设置父路由组参数
【发布时间】:2023-04-04 02:26:01
【问题描述】:

如何从子路由组设置父路由参数?可以吗? 我的参数是 {locale} ,子路由是 projects.single 。 我试过这段代码,但它不起作用:

Route::group([
'prefix' => '{locale}',
'where' => ['locale' => '[a-zA-Z]{2}'],
'middleware' => 'setlocale'
], function () {
   Auth::routes();
   Route::get('/', function () {
     return view('welcome');
   });
   Route::get('/home', 'HomeController@index')->name('home');
   Route::group(['prefix' => '/projects', 'as' => 'projects.'], function () {
      Route::get('/', 'ProjectController@index')->name('archive');
      Route::get('/{slug}', 'ProjectController@showSingle')->name('single');
   });
});

在 header.blade.php 视图中,我将此代码用于链接 href:

{{-- @foreach (config('app.available_locales') as $locale)
     <li class="nav-item">
        <a class="nav-link" href="{{ route(\Illuminate\Support\Facades\Route::currentRouteName(), $locale) }}"
                       @if (app()->getLocale() == $locale) style="font-weight: bold; text-decoration: underline" @endif>{{ strtoupper($locale) }}</a>
     </li>
 @endforeach--}}

此代码给出此错误消息缺少 [Route: projects.single] [URI: {locale}/projects/{slug}] 的必需参数。 (查看:/home/wa/pi/resources/views/includes/header.blade.php)(查看:/home/wa/pi/resources/views/includes/header.blade.php)(查看:/home/ wa/pi/resources/views/includes/header.blade.php)

【问题讨论】:

  • 怎么样:route(\Illuminate\Support\Facades\Route::currentRouteName(), ['locale' =&gt; $locale])
  • 我将 href 链接提供给相同的当前路线但使用不同的语言 - 使用 Route::currentRouteName() 方法
  • 但是当我使用这个函数时,它会返回像'project.single'这样的子规则,其中不存在'local'前缀。

标签: php laravel routes laravel-blade


【解决方案1】:

我现在不知道如何在子路由中设置参数,但是您的 {locale} 参数在您的控制器中的所有方法中都可用。 例如

public function ($locale) { // $locale is the same with {locale} in your route
    // something
}

【讨论】:

    猜你喜欢
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 2017-10-25
    • 2016-03-05
    • 2017-11-21
    • 2015-12-27
    • 2016-12-08
    相关资源
    最近更新 更多