我对中间件组中的单参数url或路由没有问题。但是当有两个参数(lang和id)时,例如show或edit,我会收到错误。代码如下。我在哪里犯错误?
文件路径app / Http / Middleware
Locate.php
<?php
namespace App\Http\Middleware;
use Closure;
class Locale
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($request->method() === 'GET') {
$segment = $request->segment(1);
if (!in_array($segment, config('app.locales'))) {
$segments = $request->segments();
$fallback = session('locale') ?: config('app.fallback_locale');
$segments = array_prepend($segments, $fallback);
return redirect()->to(implode('/', $segments));
}
session(['locale' => $segment]);
app()->setLocale($segment);
}
return $next($request);
}
}
路由文件web.php
Auth::routes();
Route::prefix('{lang?}')->middleware('locale')->group(function($lang=null) {
Route::get('/', 'HomeController@index');
Route::get('reservation-edit/{id}', 'ReservationController@reservationEdit');
});
刀片文件
{{ route ('reservations.update', ['lang'=> $lang, 'id' => $reservation->id]) }}
控制器文件
public function reservationEdit($id){
$check = Reservation::find($id);
$this->authorize('view', $check);
$hotels = DB::table('hotels')->select('hotel_name', 'id')->get();
return view('reservations.reservation-edit', ['reservation' => Reservation::findOrFail($id)], ['hotels' => $hotels]);
}
答案 0 :(得分:1)
在中间件“语言环境”中,使用ignoreParameter将ID参数仅传递给控制器。
app()->setLocale($request->segment(1));
$request->route()->forgetParameter('locale');
return $next($request);