使用 WITH [Laravel] 中的两个参数重定向路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25460068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:59:13  来源:igfitidea点击:

Redirect route with two parameters in WITH [Laravel]

phplaravelframeworks

提问by user3809590

I have problem to pass two varialbles with "with" in Redirect::route... Here is my code...

我在 Redirect::route 中传递两个带有“with”的变量时遇到问题……这是我的代码……

How to do this

这该怎么做

return Redirect::route('cart-success')->with(
            array(
                'cartSuccess' => 'You successfuly ordered. To track your order processing check your email', 
                'cartItems' => Cart::contents()
            )
        );

Here is error:

这是错误:

Undefined variable: cartItems (View: C:\xampp\htdocs\laravel-webshop\laravel\app\views\cart-success.blade.php)

未定义变量:cartItems(查看:C:\xampp\htdocs\laravel-webshop\laravel\app\views\cart-success.blade.php)

Route::group(array('before' => 'csrf'), function() {
    //Checkout user POST
    Route::post('/co-user', array(
        'as' => 'co-user-post',
        'uses' => 'CartController@postCoUser'
    ));
});

CONTROLLER

控制器

public function postCoUser() {
    $validator = Validator::make(Input::all(), array(
        'cardholdername' => 'required',
        'cardnumber' => 'required|min:16|max:16',
        'cvv' => 'required|min:3'
    ));

    if($validator->fails()) {
        return Redirect::route('checkout')
                ->withErrors($validator)
                ->withInput();
    } else {
        return Redirect::route('cart-success')->with(
            array(
                'cartSuccess' => 'You successfuly ordered. To track your order processing check your email', 
                'cartItems' => Cart::contents()
            )
        );
    }
}

View

看法

 @extends('publicLayout.main')

 @section('content')
   @if(Session::has('cartSuccess'))
    <p>{{ Session::get('cartSuccess') }}</p>

    <?php $total = 0; ?>
    @foreach ($cartItems as $cartItem)
        Name: {{ $cartItem->name }} <br>
        Price: {{ $cartItem->price }} &euro;<br>
        Quantity: {{ $cartItem->quantity }} <br>
        <?php $final = $cartItem->price * $cartItem->quantity; ?>
        Final price: {{ $final }} &euro;<br>
        <?php $total += $final; ?>
        <hr>
    @endforeach
    Total: {{ $total }} &euro;
 @endif
@stop

回答by The Alpha

You may try this:

你可以试试这个:

return Redirect::route('cart-success')
               ->with('cartSuccess', 'You successfuly ordered. To track your order processing check your email')
               ->with('cartItems', Cart::contents());

Or this:

或这个:

return Redirect::route('cart-success', array('cartSuccess' => '...', 'cartItems' => '...'));

回答by BeingCoder's

You can pass two variables like that

你可以像这样传递两个变量

$response=array('cartSuccess' => 'You have successfully ordered. To track your order processing check your email', 'cartItems' => Cart::contents());    

return Redirect::route('cart-success',$response);