开发者

What should go in CodeIgniter models?

I've always used the model as something to more or less store and execute database queries. I have heard about the fat model, thin controller concept.

The way I setup my models right now causes a lot of junk in controllers for things like val开发者_开发问答idating forms, formatting data. Does form validation, file uploading and data formatting belong in the controller or the model?

I realize this question is subjective, which should create some good discussion rather than a concrete answer.


Form Validation should definitely be part of the model. I generally represent each form as one model and pass it the sanitized post/get paramaters. The model can then take whatever action is necessary based on the input and use a property (optionally with a getter) to signal success or failure. In psuedo code you want it to look something like:

class Controller
{
    function action()
    {
        $input = new Input();
        $form = new FormModel($input);
        if ($errors = $form->errors())
        {
            //load the appropriate view for the errors
        }
        else
        {
            //load the appropriate view for success with an optional redirect
        }
}


You have two main roads to go. Thin controller/fat model or fat controller/thin model. Basicly is were you put most of the interaction. I prefer to keep at the model the major portion of the code. That way, the code is available in virtually every controller and/or lib. If the code remain at controller, it's hard (but not impossible) to use it in other controllers.

Things lije validations and other common tasks should be in a lib or helper. You can produce a set of "workers" (this is the name I give to them) to do the heavy lifting. Also, CI has a LOT of ready made libs and helpers both from the CI team and the community. Mess around the wiki to find the wealth of information available.

Hope this helps Vx


The model is what interacts with the data (most often a database). Controllers use the models to access the data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜