I instantiated the corresponding Model in the initialization method of each controller and used it in each method. I don't know what is wrong with it, and I feel that my own implementation is not elegant enough. The main reason is that the website has many modules which are similar functions. Some code replaces the model name for instantiation. {Code... I instantiate the corresponding Model in the initialization method of each controller, and then use it in each method. I don't know what is wrong with this, and I feel that my own implementation is not elegant enough. The main reason is that the website has many modules which are similar functions. Some code replaces the model name for instantiation.
public function __construct() { parent::__construct(); $this->model = new \Line(); }public function store(){ $input = \Input::all(); $validator = \Validator::make($input,$this->model->getRules('create'),$this->model->getMessage()); if ($validator->fails()) { return \Redirect::back()->with('errorCode',1)->withErrors($validator)->withInput(); }else{ if($model = $this->model->create($input)){ return \Redirect::back()->with('errorCode',0); }else{ return \Redirect::back()->with('errorCode',2); } }}
Reply content:
I instantiated the corresponding Model in the initialization method of each controller and used it in each method. I don't know what is wrong with it, and I feel that my own implementation is not elegant enough. The main reason is that the website has many modules which are similar functions. Some code replaces the model name for instantiation.
public function __construct() { parent::__construct(); $this->model = new \Line(); }public function store(){ $input = \Input::all(); $validator = \Validator::make($input,$this->model->getRules('create'),$this->model->getMessage()); if ($validator->fails()) { return \Redirect::back()->with('errorCode',1)->withErrors($validator)->withInput(); }else{ if($model = $this->model->create($input)){ return \Redirect::back()->with('errorCode',0); }else{ return \Redirect::back()->with('errorCode',2); } }}
This is not required if it is a Model.
You can write it as follows:
public function store(){ $input = \Input::all(); $validator = \Validator::make($input, Line::getRules('create'), Line::getMessage()); if ($validator->fails()) { return \Redirect::back()->with('errorCode',1)->withErrors($validator)->withInput(); }else{ if($model = Line::create($input)){ return \Redirect::back()->with('errorCode',0); }else{ return \Redirect::back()->with('errorCode',2); } }}
If the getRules and getMessage of Lind have no relationship with the specific object, it is written as a static method.
The following create directly uses the static method.