关于在何处放置全局辅助函数的 OOP/MVC 建议

发布于 2024-09-01 01:48:40 字数 1143 浏览 3 评论 0 原文

我的网站上有几个控制器正在处理表单数据。这些表单使用 AJAX,并且我在不同的控制器上有很多方法,这些方法必须执行一些特定的处理才能以 JSON 编码格式返回错误 - 请参阅下面的代码。显然这不是 DRY,我需要将此代码移动到一个可以全局使用的辅助函数中,但我想知道它实际上应该放在哪里!我应该创建一个包含此函数的静态帮助器类(例如 Validation::build_ajax_errors()),或者因为此代码生成特定于应用程序并绑定到我正在使用的 jQuery 验证插件的格式,它应该是静态的例如,存储在我的主网站控制器中的方法是从表单处理控制器扩展而来的?

               //if ajax request, output errors
                if (request::is_ajax())
                {
                    //need to build errors into array form for javascript validation - move this into a helper method accessible globally
                    $errors = $post->errors('form_data/form_error_messages');

                    $i = 0;
                    $new_errors  = array();
                    foreach ($errors as $key => $value)
                    {
                        $new_errors[$i][0] = '#' . $key;
                        $new_errors[$i][1] = $value;
                        $new_errors[$i][2] = "error";
                        $i++;
                    }

                    echo '{"jsonValidateReturn":' . json_encode($new_errors) . '}'; 

                    return;
                }

I have a couple of controllers on my site which are handling form data. The forms use AJAX and I have quite a few methods across different controllers which are having to do some specific processing to return errors in a JSON encoded format - see code below. Obviously this isn't DRY and I need to move this code into a single helper function which I can use globally, but I'm wondering where this should actually go! Should I create a static helper class which contains this function (e.g Validation::build_ajax_errors()), or as this code is producing a format which is application specific and tied into the jQuery validation plugin I'm using, should it be a static method stored in, for example, my main Website controller which the form handling controllers extend from?

               //if ajax request, output errors
                if (request::is_ajax())
                {
                    //need to build errors into array form for javascript validation - move this into a helper method accessible globally
                    $errors = $post->errors('form_data/form_error_messages');

                    $i = 0;
                    $new_errors  = array();
                    foreach ($errors as $key => $value)
                    {
                        $new_errors[$i][0] = '#' . $key;
                        $new_errors[$i][1] = $value;
                        $new_errors[$i][2] = "error";
                        $i++;
                    }

                    echo '{"jsonValidateReturn":' . json_encode($new_errors) . '}'; 

                    return;
                }

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

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

发布评论

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

评论(1

浅笑轻吟梦一曲 2024-09-08 01:48:40

恕我直言,您的问题归结为个人编程风格的问题。当我确定一段需要重构以符合 DRY 原则的代码时,我并不总是以相同的方式重构它。

如果我理解正确,您的表单类中需要此函数/方法,并且所有这些表单类都扩展相同的基本表单类。在这种情况下,我会将有问题的方法提升到超类中(我知道您提到您希望该方法“全局”可用,但您的用例的其余部分似乎暗示该函数/方法只能是在您的表单类中使用。)。

当我实际上有一个需要在整个应用程序中可用的辅助函数时(并且我确信没有其他地方可以放置它),我将其放置在应用程序的入口点中。使用 Zend Framework 的 MVC,该入口点将是公共index.php(所有请求都通过index.php 路由,使其成为全局函数的逻辑位置)。在该文件的底部,我总是添加一些辅助函数,以便在调试时节省一些打字时间。请参阅“您将使用过的最有用的功能......”作为示例。

IMHO, your question boils down to a question of personal programming style. When I've identified a piece of code that needs to be refactored in order to conform to the DRY principal, I don't always refactor it in the same way.

If I understand correctly, you need this function/method in your form classes, and all of those form classes extend the same base form class. In that case, I would promote the method in question into the superclass (I know that you mentioned that you wanted the method to be available "globally," but the rest of your use case seemed to imply that the function/method would only be used in your form classes.).

When I actually have a helper function that I need available throughout the application (and I'm for sure that there's nowhere else to put it), I place it in the application's entry point. Using the Zend Framework's MVC, that entry point would be the public index.php (all requests are routed through index.php, making it the logical place for global functions). At the bottom of that file I always add some helper functions to save myself some typing while debugging. See "The most useful function you will ever use . . ." as an example.

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