MVC 中放置逻辑的正确位置

发布于 2024-11-02 17:38:37 字数 1203 浏览 0 评论 0原文

我正在使用 yii 框架编写一个 mvc Web 应用程序。我有一段业务逻辑,但不确定将其放置在哪里。 $usernameid=$model->random_id_gen('5'); 是我正在谈论的函数。

SiteController:

<!-- snip -->
      public function actionIndex()
       {
         $model = new Users();

         if (isset($_POST['Users'])) {
            //call the active record table model
            $model = new Users();

            //massively assign attributes
            $model->attributes=$_POST['Users'];

            //generate random userid
            $usernameid=$model->random_id_gen('5');


<!-- snip -->

Users Active Record 类:

 <!-- snip -->
  public function random_id_gen($length)
    {
        $characters = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
        $max = strlen($characters) - 1;
        $string = '';

        for ($i = 0; $i < $length; $i++) {
            $string .= $characters[mt_rand(0, $max)];
        }

        return $string;
    }

我的问题:这个 id 生成器函数是否属于 Active record 模型?它应该在控制器中吗?它应该在一个单独的模型中,因为它是“业务逻辑”,但与数据库关系不大?

我正在努力避免“臃肿”我的 MVC 类。提前谢谢大家了。

更新 我正在寻找 yii 特定的解决方案。看来问题已经发展成“如果有这样的约定,应该把库放在 yii 的哪里”。

I'm writing an mvc web app with the yii framework. I have a piece of business logic and I'm unsure where to place it. $usernameid=$model->random_id_gen('5'); is the function I'm talking about.

SiteController:

<!-- snip -->
      public function actionIndex()
       {
         $model = new Users();

         if (isset($_POST['Users'])) {
            //call the active record table model
            $model = new Users();

            //massively assign attributes
            $model->attributes=$_POST['Users'];

            //generate random userid
            $usernameid=$model->random_id_gen('5');


<!-- snip -->

Users Active Record Class:

 <!-- snip -->
  public function random_id_gen($length)
    {
        $characters = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
        $max = strlen($characters) - 1;
        $string = '';

        for ($i = 0; $i < $length; $i++) {
            $string .= $characters[mt_rand(0, $max)];
        }

        return $string;
    }

My question: Does this id generator function belong in the Active record model? Should it be in the controller? Should it be in a separate model since it is "business logic", but has little to do with databases?

I'm trying to get better at not "bloating" my MVC classes. Thanks ahead of time guys.

UPDATE
I'm looking for a yii specific solution. It seems the question has developed into "where should one put libraries in yii" if there is such a convention.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暖阳 2024-11-09 17:38:37

MVC 应重命名为 MVCL .. 即模型-视图-控制器-库。这些类型的函数应该放入单独的库系统中。

控制器应位于顶部。它有两个主要功能:接受用户输入和协调其他元素(控制器/库/视图)以进行输出。传统上,控制器向模型询问数据。但数据操作(最常见)应该由库中的函数完成。

对于最常见的操作,可能有两层库,一层可能是复杂的(基于对象),另一层可能是简单的(基于全局函数)。像剥离标签、或者准备好数据 XML、清理 URL 等之类的事情。整个系统可以访问简单的函数库,而无需实例化特定的库对象。

有道理吗?

MVC should be renamed to MVCL .. that is Model-View-Controller-Library. These kind of functions should go into a separate system of libraries.

Controller should be on the TOP. It has two major functions : accepting user input and coordinating other elemnts (controller / library/view) for output. traditionally, Controller asks data from the model. But data manipulation (most common) should be done by functions in the library.

There could be two levels of libraries, one could be complex (object based) and other could be simple (global functions based) for the most common operations. Something like stripping out tags, OR making the data XML ready, cleaning an URL etc.. simple functions libraries can be accessed by the whole system without any instantiation of the a particular library object.

Makes sense ?

故笙诉离歌 2024-11-09 17:38:37

我习惯将库:放在

  • 受保护/组件中通用库的扩展(/ protected/extensions)中,
  • 项目通用,
  • 如果该库对于受保护/模块/xxx/组件中的
  • 则将模块特定库放在供应商目录中,如果该库是巨大,我不想对其进行版本控制。

通常您会导入 protected/components (Yii::import('application.components.*') 目录,这样这些库就可以按需使用。

这对我来说效果很好。

I use to put libraries :

  • in an extension (/protected/extensions) for generic libraries
  • in protected/components if the library is generic to the project
  • in protected/modules/xxx/components for module specific libraries
  • in a vendors directory if the library is huge and I don't want to version it

Usually you import the protected/components (Yii::import('application.components.*') directory so these libraries would be available on demand.

This works fine for me.

扎心 2024-11-09 17:38:37

我知道这很煽动性。但由于此特定功能与数据库或操作员逻辑(控制器)无关,也不与它们交互,因此它根本不属于任何一个对象。而且它是一个简单的实用函数

所以这就是它应该去的地方。 (除非你故意做CCP。)

I know that's quite seditionary. But since this specific functionality is not related to the database or operator logic (controller) and does neither interact with them, it simply does not belong in either object. Moreover it's a simplistic utility function.

So this is where it should go. (Unless you are intentionally doing CCP.)

情栀口红 2024-11-09 17:38:37

据我所知,我自己也一直在使用这个,Yii 有一个辅助文件夹,那是我用来放置辅助函数的地方,就像你问的那样。我没有经常使用它,因为 yii 涵盖了太多的功能,但我必须添加一个时间函数来显示事件的时间,直到/之前,这在帮助文件夹中就像一个魅力,最好的部分是 Yii 准确地知道当我使用 Yii::import 时去哪里查看...希望有帮助

As far as I know, and I have been using this myself, Yii Has a helper folder and thats the place I use to put helper functions like the one you asked about. I havent used it much since yii has sooo many functions covered, but I had to add a time function that would show the time until/ago for an event, that worked like a charm in the helper folder, and best part was Yii knew exactly where to look when I used Yii::import... hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文