英文:
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('/checkout', [CheckoutController::class, 'index'])->name('checkoutIndex')->middleware('auth');
My 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]);
}
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:
<a class="btn btn-primary text-light p-3 rounded-0" href="{{ route('checkoutIndex', App::getLocale()) }}">{{__('Proceed to Checkout')}}</a>
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->expectsJson()) {
return route('login');
}
}
You need to pass the required parameter to the route:
return route('login', ['lang' => 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:
<a class="btn btn-primary text-light p-3 rounded-0" href="{{ route('checkoutIndex', App::getLocale()) }}">{{__('Proceed to Checkout')}}</a>
to
<a class="btn btn-primary text-light p-3 rounded-0" href="{{ route('checkoutIndex', ['lang' => App::getLocale()) ] }}">{{__('Proceed to Checkout')}}</a>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论