【问题标题】:2 parameters id and lang route middleware locale group don't work - Laravel 5.52 个参数 id 和 lang route 中间件语言环境组不起作用 - Laravel 5.5
【发布时间】:2018-11-17 20:56:44
【问题描述】:

我对中间件组中的单参数 url 或路由没有问题。但是当有两个参数(lang 和 id)(例如 show 或 edit)时出现错误。代码如下。我哪里出错了?

文件路径app/Http/中间件

定位.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]);
    }

【问题讨论】:

  • “不工作”是什么意思?什么不工作?您期望发生的事情不是?
  • 您好 Felippe,我的刀片路由有问题,我删除了 'lang' 属性,然后配置控制器文件添加两个参数 $lang=null,$id。现在可以了。但不健康。我继续。谢谢,最好的问候

标签: php laravel laravel-5.5


【解决方案1】:

在您的中间件“区域设置”中,使用 forgetParameter 仅将 id 参数传输到您的控制器。

app()->setLocale($request->segment(1));
$request->route()->forgetParameter('locale');
return $next($request);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-13
    • 2018-07-09
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多