Laravel从子路由设置父路由组参数

时间:2019-06-29 12:45:26

标签: php laravel routes laravel-blade

如何从子路由组设置父路由参数? 我的参数是{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)

1 个答案:

答案 0 :(得分:0)

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

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