Laravel 4:将从存储库获得的验证消息传递给控制器 [英] Laravel 4: Pass validation messages obtained from repository to controller

查看:84
本文介绍了Laravel 4:将从存储库获得的验证消息传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

了解Ioc和存储库,并陷入最后的障碍!

Learning about Ioc and Repositories and stuck at last hurdle!

假设我正在验证输入,如何将消息从存储库中的Validator传递回控制器?

Assuming I am validating input, how do I pass back messages from the Validator within the repository to the controller?

UserRepository

UserRepository

interface UserRepository {
    public function all();
    public function create($input);
    public function findById($id);
}

Sentry2UserRepository

Sentry2UserRepository

class Sentry2UserRepository implements UserRepository {
...
public function create($input) {
        $validation = Validator::make($input, User::$rules);
        if ($validation->passes()) {
    Sentry::createUser( array_except( $input, ['password_confirmation']));

            // Put something here to tell controller that user has been successfully been created
            return true;
        }
        else {
            // pass back to controller that validation has failed
            // with messages
            return $validation->messages(); ?????     
        }       
...

我的UserController

My UserController

UserController extends BaseController {
    ...
    public function postRegister() {
    $input['first_name'] = Input::get('first_name');
    $input['last_name'] = Input::get('last_name');
    $input['email'] = Input::get('email');
    $input['password'] = Input::get('password');
    $input['password_confirmation'] = Input::get('password_confirmation');


        // Something like
        if ($this->user->create($input)) {
            Session::flash('success', 'Successfully registered');
            return Redirect::to('/');
        }
        else {
            Session::flash('error', 'There were errors in your submission');
            return Redirect::to('user/login')->withErrors()->withInput();
        }
    }
    ...
}

Laravel刚进入1.5周,所以请对我轻松一点.

Only 1.5 weeks into Laravel so please go easy on me.

推荐答案

假设您的存储库对您来说已经可以正常工作了:

Assuming your repository is working fine for you already:

class Sentry2UserRepository implements UserRepository {
    public $validation;

    public function create($input) {
            $this->validation = Validator::make($input, User::$rules);
            if ($this->validation->passes()) {
                Sentry::createUser( array_except( $input, ['password_confirmation']));

                // Put something here to tell controller that user has been successfully been created
                return true;
            }
            else {
                // pass back to controller that validation has failed
                // with messages
                return false;     
            }
    }     

}

然后,您只需要在控制器中使用

Then you just have to access it within your controller using

$this->user->validation->messages()

这篇关于Laravel 4:将从存储库获得的验证消息传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆