如何将参数传递给中间件?

huangapple go评论59阅读模式
英文:

How to pass a parameter to a middleware?

问题

我在我的routes/web.php文件中有这个路由:

Route::get('/checkout', [CheckoutController::class, 'index'])->name('checkoutIndex')->middleware('auth');

我的CheckoutController如下:

function index()
{
    if (Cart::instance('default')->count() == 0) {
        return redirect()->route('cartIndex', App::getLocale())->withErrors('Your shopping cart is empty! Please select an item to checkout.');
    }

    $discount = session()->has('coupon') ? session()->get('coupon')['discount'] : 0;

    return view('checkout_index')->with(['lang' => App::getLocale(), 'discount' => $discount]);
}

当我未登录并访问URL时,它会带我到登录页面,这是应该的。但登录页面出现了一个Missing required parameter for [Route: login] [URI: {lang}/login] [Missing parameter: lang].错误。

登录页面在应用程序的其他任何位置的自己的路由中都能正常工作,只有在点击这个特定链接时才会出现错误:

<a class="btn btn-primary text-light p-3 rounded-0" href="{{ route('checkoutIndex', App::getLocale()) }}">{{__('Proceed to Checkout')}}</a>

我认为使用中间件导致了这个问题。

我正在使用内置的Laravel认证。登录的路由是Auth::routes();

有什么想法吗?

英文:

I have this route in my routes/web.php:

Route::get(&#39;/checkout&#39;, [CheckoutController::class, &#39;index&#39;])-&gt;name(&#39;checkoutIndex&#39;)-&gt;middleware(&#39;auth&#39;);

My CheckoutController:

 function index()
{
    if (Cart::instance(&#39;default&#39;)-&gt;count() == 0) {
        return redirect()-&gt;route(&#39;cartIndex&#39;, App::getLocale())-&gt;withErrors(&#39;Your shopping cart is empty! Please select an item to checkout.&#39;);
    }

    $discount = session()-&gt;has(&#39;coupon&#39;) ? session()-&gt;get(&#39;coupon&#39;)[&#39;discount&#39;] : 0;

    return view(&#39;checkout_index&#39;)-&gt;with([&#39;lang&#39; =&gt; App::getLocale(), &#39;discount&#39; =&gt; $discount]);
}

When I'm not logged in and go to the URL it takes me to a login page as it should. but the login page gives a Missing required parameter for [Route: login] [URI: {lang}/login] [Missing parameter: lang]. error.

The login page works well in its own route anywhere else in the app, I only get the error by clicking on this one specific link:

&lt;a class=&quot;btn btn-primary text-light p-3 rounded-0&quot; href=&quot;{{ route(&#39;checkoutIndex&#39;, App::getLocale()) }}&quot;&gt;{{__(&#39;Proceed to Checkout&#39;)}}&lt;/a&gt;

I believe having middleware is causing the issue.

I'm using the laravel built-in auth. the route to login is Auth::routes();

Any ideas?

答案1

得分: 1

在你的 App\Http\Middleware\Authenticate 中间件中,你需要尝试以下更改:

protected function redirectTo($request)
{
    if (! $request->expectsJson()) {
        return route('login');
    }
}

你需要将必需的参数传递给路由:

return route('login', ['lang' => App::getLocale()])
英文:

could you try this

in your App\Http\Middleware\Authenticate middleware

protected function redirectTo($request)
{
    if (! $request-&gt;expectsJson()) {
        return route(&#39;login&#39;);
    }
}

You need to pass the required parameter to the route:

return route(&#39;login&#39;, [&#39;lang&#39; =&gt; App::getLocale()])

答案2

得分: 0

更改这行代码:

<a class="btn btn-primary text-light p-3 rounded-0" href="{{ route('checkoutIndex', ['lang' => App::getLocale()]) }}">{{__('Proceed to Checkout')}}</a>

英文:

change this line:

&lt;a class=&quot;btn btn-primary text-light p-3 rounded-0&quot; href=&quot;{{ route(&#39;checkoutIndex&#39;, App::getLocale()) }}&quot;&gt;{{__(&#39;Proceed to Checkout&#39;)}}&lt;/a&gt;

to

&lt;a class=&quot;btn btn-primary text-light p-3 rounded-0&quot; href=&quot;{{ route(&#39;checkoutIndex&#39;, [&#39;lang&#39; =&gt; App::getLocale()) ] }}&quot;&gt;{{__(&#39;Proceed to Checkout&#39;)}}&lt;/a&gt;

huangapple
  • 本文由 发表于 2023年2月19日 15:51:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定