OOP / MVC recommendations on where to place the global helper function

I have a couple of controllers on my site that process form data. Forms use AJAX, and I have several methods for different controllers that have to do some processing to return errors in JSON encoded format - see the code below. Obviously, this is NOT DRY, and I need to move this code into one helper function that I can use globally, but I wonder where it should go! Should I create a static helper class that contains this function (e.g. Validation :: build_ajax_errors ()), or since this code creates a format that is application specific and related to the jQuery validation plugin that I use, should it be static method, stored, for example, in my main site controller,from which form controllers are distributed?

               //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;
                }
+3
1

, . , , , .

, / , . ( , , , "", , / .).

, ( , ), . Zend Framework MVC, index.php( index.php, ). , . . " , - ..." .

+2

Source: https://habr.com/ru/post/1744910/


All Articles