In Laravel, what is wrong with the method for the Controller to instantiate the model?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.