在新函数LARAVEL中重构代码的一部分

时间:2016-09-29 10:03:12

标签: laravel

我需要重构这段代码。

我需要将验证代码放在一个新函数中,并在另一个函数中使用该函数。 这段代码

public function comprobarValidacion(\Request $request)
        {
             $validador =  $this->validator($request::all());
               $errors = $validador->errors()->all();

           if(count($errors)):

             return Redirect()->back()->withErrors($validador);

            endif;
        }

我尝试在每个函数中调用它

public function postLogin(\Request $request)
{

     $this->comprobarValidacion($request::all());
     return Redirect()->back();
} 

但它不起作用。代码,但是如果我把comprobarValidacion内容放在任何函数中

这是完整的代码;

class AuthController extends Controller {


            use AuthenticatesAndRegistersUsers, ThrottlesLogins;

            protected $auth;

             protected $redirectTo = '/dashboard';

             //despues de loguear

             protected $redirectAfterLogout = '/paginaporcrear';


            public function __construct(Guard $auth)
            {
              //  $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
              //  $this->middleware($auth);

                $this->auth = $auth;


            }


            protected function validator(array $data)
            {

                $reglas =  [
                    'name' => 'required|max:255',
                    'email' => 'required|email|max:255|unique:users',
                    'password' => 'required|min:1|confirmed',
                          ];
                $mensajes =    [
                    'required' => 'El campo :attribute es requerido',
                    'email' => 'El campo :atribute tiene que tener formato de email',
                            ];

                return  Validator::make($data, $reglas, $mensajes);
            }


            public function comprobarValidacion(\Request $request)
            {
                 $validador =  $this->validator($request::all());
                   $errors = $validador->errors()->all();

               if(count($errors)):

                 return Redirect()->back()->withErrors($validador);

                endif;
            }


            public function getRegistro()
            {
                return view('auth.register');
            }


            public function postRegistro(\Request $request)
            {
                    $validador =  $this->validator($request::all());

                   $errors = $validador->errors()->all();

                   if(count($errors)>0):

                     return Redirect()->back()->withErrors($validador);

                    endif;


                $this->create($request::all());



                \Session::flash('creado','El usuario '.$request::get('name').' ha sido creado exitosamente');
                return Redirect()->back();

            }

            public function getLogin()
            {

                return view('auth.login');
            }

            public function postLogin(\Request $request)
            {
                     // $validador =  $this->validator($request::all());
                     // $errors = $validador->errors()->all();

                     //   if(count($errors)):

                     //     return Redirect()->back()->withErrors($validador);

                     //    endif;


                 $this->comprobarValidacion($request::all());
                 return Redirect()->back();
            } }

¿我该怎么做?

2 个答案:

答案 0 :(得分:0)

当您在comprobarValidacion函数中执行返回操作时,您将结果返回到操作。我将看看注册区域如何在laravel(Auth / RegisterController)

中完成它

它返回验证器,然后在控制器操作中运行 - > validate方法。 Laravel自动处理对操作中的validate()调用失败,并在异常处理程序中调用convertValidationExceptionToResponse

CONCATENATE(R1C1,R2C1,R3C1)

然后在注册动作中

protected function validator(array $data)
{
    // Notice how it doesn't call ->validate()
    return Validator::make($data, [
        'name'     => 'required|max:255',
        'email'    => 'required|email|max:255|unique:users',
        'password' => 'required|min:6|confirmed',
    ]);
}

答案 1 :(得分:0)

创建规则

public function add_user_rules(array $data)
{
  $messages = [
    'email.required' => 'Please enter email.'
  ];

  $validator = Validator::make($data, [
    'email' => 'required|max:30|unique:email',
    'name' => 'required'
  ], $messages);

  return $validator;
}


public function postAdd(Request $request)
{
  $request_data = $request->all();
  $validator = $this->add_user_rules($request_data);
  if($validator->fails())
  {
    return redirect()->back()->withErrors($validator)->withInput();
  }
  else
  {  
    //add 
  }       
}