Php Laravel 4:将从存储库获得的验证消息传递给控制器
学习国际奥委会和知识库,并在最后的障碍 假设我正在验证输入,如何将消息从存储库中的验证器传回控制器 用户存储库Php Laravel 4:将从存储库获得的验证消息传递给控制器,php,validation,laravel,inversion-of-control,laravel-4,Php,Validation,Laravel,Inversion Of Control,Laravel 4,学习国际奥委会和知识库,并在最后的障碍 假设我正在验证输入,如何将消息从存储库中的验证器传回控制器 用户存储库 interface UserRepository { public function all(); public function create($input); public function findById($id); } class Sentry2UserRepository implements UserRepository { ... public
interface UserRepository {
public function all();
public function create($input);
public function findById($id);
}
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(); ?????
}
...
Sentry2UserRepository
interface UserRepository {
public function all();
public function create($input);
public function findById($id);
}
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();
}
}
...
}
只有1.5周进入Laravel,所以请对我放松。假设您的存储库已经为您运行良好:
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()