我有一个错误:
缺少路由所需的参数: shop.show。(浏览: C:\xampp\htdocs\frile\resources\views\inc\header.blade.php)
我想我明白我的问题了,但我不确定。
我正试图通过一条资源路线访问我的产品的详细页面。如果我没有语言转换器的话,它可以正常工作。
我的语言转换需要当前的路由,这样他就可以在前面添加“en”或“nl”。但是我的资源路由'shop.show‘需要一个参数,而交换机不能给他它。
有解决办法吗?这是我的密码:
路线:
Route::redirect('/', '/en');
Route::group(['prefix' => '{language}'], function () {
/* pages */
Route::get('/', [PagesController::class, 'index']) -> name('home');
/* shop is in the products section */
Route::get('/projects', [PagesController::class, 'projects']) -> name('projects');
Route::get('/about', [PagesController::class, 'about']) -> name('about');
Route::get('/contact', [PagesController::class, 'contact']) -> name('contact');
/* products */
Route::resource('shop', ProductController::class);
});控制器(我给出了它的参数产品,这样我的语言转换程序就不会抓狂):
public function show($product, $id)
{
$product = Product::find($id);
return view('pages.shop.product', [app()->getLocale(), 'product' => $product]);
}header.blade.php (我的语言转换程序在这里):
<header>
<h1><a href="{{ route('home', app()->getLocale()) }}">FRILE</a></h1>
<nav>
<ul>
<li><a href="{{ route('shop.index', app()->getLocale()) }}">{{ __('nav.shop') }}</a></li>
<li><a href="{{ route('projects', app()->getLocale()) }}">{{ __('nav.projects') }}</a></li>
<li><a href="{{ route('about', app()->getLocale()) }}">{{ __('nav.about') }}</a></li>
<li><a href="{{ route('contact', app()->getLocale()) }}">{{ __('nav.contact') }}</a></li>
<ul id="languageSwitcher">
<li><a href="{{ route(Route::currentRouteName(), 'en') }}">en</a></li>
<li><a href="{{ route(Route::currentRouteName(), 'nl') }}">nl</a></li>
</ul>
</ul>
</nav>
</header>shop.blade.php (在这里,我链接到详细页面):
@extends('layouts.app')
@section('content')
<h2>shop</h2>
<a href="{{ route('shop.create', app()->getLocale()) }}">create product</a>
@if (count($products) > 0)
@foreach ($products as $product)
<p><a href="{{ route('shop.show', [app()->getLocale(), $product -> id]) }}">{{ $product -> name }}</a></p>
@endforeach
{{ $products->links() }}
@else
<p>No products yet</p>
@endif
@endsectionsetLanguage中间件:
public function handle(Request $request, Closure $next)
{
App::setLocale($request->language);
return $next($request);
}发布于 2020-11-26 00:17:32
根据显示方法,您接受两个参数,$product和$id,从显示方法中移除$product,因为您没有指定它在函数中要做什么,也没有将它传递给视图,我认为这就是问题所在,您可以这样离开它。
public function show ($id)
{
...
}发布于 2020-11-26 02:01:41
好的,所以我这样做有点糟糕,我只是在我的header.blade.php中设置了一个isset和空值,这很有效,但是如果有人知道更好的答案,lmk。
header.blade.php:
<header>
<h1><a href="{{ route('home', app()->getLocale()) }}">FRILE</a></h1>
<nav>
<ul>
<li><a href="{{ route('shop.index', app()->getLocale()) }}">{{ __('nav.shop') }}</a></li>
<li><a href="{{ route('projects', app()->getLocale()) }}">{{ __('nav.projects') }}</a></li>
<li><a href="{{ route('about', app()->getLocale()) }}">{{ __('nav.about') }}</a></li>
<li><a href="{{ route('contact', app()->getLocale()) }}">{{ __('nav.contact') }}</a></li>
<ul id="languageSwitcher">
@isset($product)
<li><a href="{{ route(Route::currentRouteName(), ['en', $product->id]) }}">en</a></li>
<li><a href="{{ route(Route::currentRouteName(), ['nl', $product->id]) }}">nl</a></li>
@endisset
@empty($product)
<li><a href="{{ route(Route::currentRouteName(), 'en') }}">en</a></li>
<li><a href="{{ route(Route::currentRouteName(), 'nl') }}">nl</a></li>
@endempty
</ul>
</ul>
</nav>
</header>发布于 2021-05-20 12:52:57
如果要传递额外的参数,如: Shop.blade.php file=>
<p><a href="{{ route('shop.show', [app()->getLocale(), $product -> id]) }}">{{ $product -> name }}</a></p>使用app()-.getLocale()作为第二个参数:
<p><a href="{{ route('shop.show', [$product ->
id,app()->getLocale()]) }}">{{ $product -> name }}</a></p>也许这对你有用。
https://stackoverflow.com/questions/65013850
复制相似问题