2

Ioc と Repositories について学び、最後のハードルに行き詰まった!

入力を検証していると仮定すると、リポジトリ内の Validator からコントローラーにメッセージを返すにはどうすればよいですか?

ユーザーリポジトリ

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

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 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 週間しか経っていません。

4

1 に答える 1

0

リポジトリがすでに正常に機能していると仮定します。

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;     
            }
    }     

}

次に、次を使用してコントローラー内でアクセスするだけです

$this->user->validation->messages()
于 2013-09-29T02:23:47.967 に答える