微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

控制器:

<?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!";
        }

    }
}

路线:

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'));

在视图中:

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

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

URL看起来像:

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

提前致谢

解决方法:

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐