简体   繁体   English

雄辩的ORM控制器或模型

[英]eloquent ORM controller or model

I am writing a web application, and I am a self proclaimed disorganised developer, I write things quickly and worry about maintenance later. 我正在编写一个Web应用程序,并且我是一个自称为杂乱无章的开发人员,我写得很快,以后又担心维护。 I am working with laravel at the moment, I have quite alot of database interaction, all the examples of eloquent seem to interacting with database object directly in the controller. 目前,我正在与laravel合作,我进行了大量的数据库交互,雄辩的所有示例似乎都直接在控制器中与数据库对象进行交互。 Is this the best option, or is it more organised to wrap these eloquent methods in a function in the Model for that relevant query. 这是最好的选择,还是组织得更好,以便将这些雄辩的方法包装在模型中用于该相关查询的函数中。 eg 例如

class HomeController extends \BaseController {

    public function index()
    {
         $user = User::find(1);
         return $user;
    }

}

would this be better served as, 这样做会更好吗,

class HomeController extends \BaseController {

    public function index()
    {
         $user = new User;
         $result = $user->getSingleUser(1); //Being a method in the User.php model
         return $result;
    }

}

I realise this is very very basic example, but for organisational purposes is it best to seperate the database and "business logic" away from the controller? 我意识到这是一个非常基本的示例,但是出于组织目的,最好将数据库和“业务逻辑”与控制器分开吗?

but for organisational purposes is it best to seperate the database and "business logic" away from the controller? 但是出于组织目的,最好将数据库和“业务逻辑”与控制器分开吗?

Yes

would this be better served as, 这样做会更好吗,

Yes - you should separate your logic into the correct areas, to make the code more manageable. 是的-您应该将逻辑分成正确的区域,以使代码更易于管理。

That's why MVC pattern is for. 这就是为什么要使用MVC模式。 Store your db requests deep inside, also you can separate your model layer from db layer if you wish. 将您的数据库请求存储在内部,如果需要,您也可以将模型层与数据库层分开。 If you more decouple your application structure then more manageable is your code. 如果您进一步分离应用程序结构,那么代码将更易于管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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