简体   繁体   中英

Laravel - extending models to controller

Is extending model to a controller a good idea?

Like say I want to make my functions in the model protected instead of public and then extend my model to the controller so that the controller can still call those functions?

Is it a good thing to do? Or should I just leave them public?

No, don't do that! That defeats the whole object of MVC. Leave all functions in the model public unless they are only to be used by that model or related models. In which case they can be private/protected respectively.

If you extend your model to the controller then the controller turns into a model.

I can't imagine how controllers could "extend" your model. You can extend Eloquent models for example, and make inherited models like:

class ModelB extends ModelA
{
 // code
}


class ModelA extends Eloquent
{
protected $something;
//code
}

and then use them in the controller accordingly. Your controllers extend a different class, BaseController and have a different purpose than models to begin with. So the short asnwer is no.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM