【问题标题】:Laravel not redirecting to routeLaravel 没有重定向到路由
【发布时间】:2014-09-16 16:34:14
【问题描述】:

我正在尝试验证表单中的一些数据并将用户重定向到页面,如果有任何错误。我的代码不会重定向到任何路线。我所有路线的路由都正常工作。我用 Input::all() 回显了输入,它确实有用户输入。验证器也可以工作。我不确定究竟是什么阻止了 Redirect::route 工作

public function postPurchase()
{
    $validator = Validator::make(Input::all(), array(
        'condition' => 'required',
        'memory' => 'required',
        'color' => 'required',
        'accessories' => 'required',
        'shipping' => 'required'
    ));

    // $input = Input::all();
    // dd($input);

    if ($validator->fails()) {
        // echo "string";
        return Redirect::route('home');

    } else {
        echo "this succedded";
    }
    //Get prices, item id, etc and send user to checkout page
    // echo "Get prices, item id, etc and send user to checkout page";
}

这是 postPurchase 方法之前的代码:

public function getPurchase()
    {
        return View::make('general.purchase');
    }

    public function getCheckout()
    {
        return View::make('general.checkout');
    }

    public function postPurchaseCheck()
    {
        $input = Input::all();
        $this->input = $input;

        if (Input::get('buy')) {
            $this->postPurchase();
        }
        elseif (Input::get('cart')) {
            $this->postAddCart();
        }

    }

【问题讨论】:

  • 您是否收到任何错误消息?你也可以发布你的routes.php文件内容吗?

标签: laravel laravel-4


【解决方案1】:

您调用该函数 - 但您不会“返回”返回给您的重定向。

改变

if (Input::get('buy')) {
            $this->postPurchase();
        }

if (Input::get('buy')) {
            return $this->postPurchase();
        }

【讨论】:

    【解决方案2】:

    尝试更新这个

    if (Input::get('buy')) {
                return $this->postPurchase();
     } elseif (Input::get('cart')) {
       return $this->postAddCart();
    }
    

     if ($validator->fails()) {
            // echo "string";
            return Redirect::to('home');
    
        } else {
            echo "this succedded";
        }
    

    也不要忘记在路由文件中定义它

    【讨论】:

      【解决方案3】:

      更改您的 postPurchaseCheck() 方法以返回 $this->postPurchase() 像这样返回的内容。因为您在内部调用 postPurchase() 方法,但 postPurchaseCheck() 方法上发生了 post,所以重定向必须由处理 POST 请求的方法返回

      public function postPurchaseCheck()
      {
          $input = Input::all();
          $this->input = $input;
      
          if (Input::get('buy')) {
             return $this->postPurchase();
          }
          elseif (Input::get('cart')) {
             return $this->postAddCart();
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2018-07-22
        • 2017-06-19
        • 2021-04-26
        • 2019-07-23
        • 2020-02-13
        • 2021-07-22
        • 2015-07-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多