laravel 通过控制器将错误传递给视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23014808/
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:21:00  来源:igfitidea点击:

laravel passing errors to views through controllers

phplaravellaravel-4

提问by spacemonkey

I am trying to validate in an action within my controller. once it fails, I send the messages resulting from the validator to the other function in the same controller to pass it to the view. The only problem is I don't see anything displaying in the view and the url changes to a weird format once I click submit and errors occurs.

我正在尝试在我的控制器中的一个操作中进行验证。一旦失败,我会将验证器产生的消息发送到同一控制器中的另一个函数,以将其传递给视图。唯一的问题是我没有看到视图中显示任何内容,并且一旦我单击提交并发生错误,url 就会更改为奇怪的格式。

Controller:

控制器:

<?php

class MembersController extends BaseController {

    protected $layout = 'layouts.master';


    public function loadRegisterView($input = null)
    {
        if($input == null)
            return View::make('members.register');
        else
            return View::make('members.register', $input);
    }

    public function loadLoginView()
    {
        return View::make('members.login');
    }


    public function register()
    {

        $rules = array(
            'first_name' => 'Required|Min:3|Max:88|Alpha',
            'last_name' => 'Required|Min:3|Max:88|Alpha',
            'password' => 'Required|Min:3|Max:88|Alpha|Confirmed',
            'email' => 'Required|email|unique:users',
        );

        $validator = Validator::make(Input::all(), $rules);


        if ($validator->fails())
        {
            $messages = $validator->messages();
            return Redirect::action('MembersController@loadRegisterView', $messages);
        }else{
            return "Thank you!";
        }

    }
}

Routes:

路线:

Route::get('login', array('as' => 'login', 'uses' => 'MembersController@loadLoginView'));

Route::get('signup', array('as' => 'signup', 'uses' => 'MembersController@loadRegisterView'));


Route::get('/', 'MainController@index');


Route::post('processRegistration', array('as' => 'processRegistration', 'uses' => 'MembersController@register'));

in the View:

在视图中:

@if ( $errors->count() > 0 )
      <p>The following errors have occurred:</p>

      <ul>
        @foreach( $errors->all() as $message )
          <li>{{ $message }}</li>
        @endforeach
      </ul>
    @endif

and the URL looks like:

网址如下所示:

http://localhost:8888/store/index.php/signup?%00%2A%00messages%5Bfirst_name%5D%5B0%5D=The+first+name+field+is+required.&%00%2A%00messages%5Blast_name%5D%5B0%5D=The+last+name+field+is+required.&%00%2A%00messages%5Bpassword%5D%5B0%5D=The+password+field+is+required.&%00%2A%00messages%5Bemail%5D%5B0%5D=The+email+field+is+required.&%00%2A%00format=%3Amessage

Thanks in advance

提前致谢

回答by Jarek Tkaczyk

You pass variable directly to the route, wile you should do:

您将变量直接传递给路线,您应该这样做:

    if ($validator->fails())
    {
        $messages = $validator->messages();
        return Redirect::action('MembersController@loadRegisterView')->withErrors($messages);
    }else{
        return "Thank you!";
    }