1

The question is straightforward. I don't prefer to write "controller code" in blade file. However, I can't convert title (user input) to formatted title directly because of locale language differences. I want to write switch case code in blade file like that:

    switch ($word_char) {
            case 'a':
            case 'A':
                $word_char_new = 'A';
                break;
            case 'b':
            case 'B':
                $word_char_new = 'B';
                break;
            case 'c':
            case 'C':
                $word_char_new = 'C';
                break;
            case 'รง':
...

Is this a good practice or should I avoid it?

horse
  • 707
  • 4
  • 11
  • 30

1 Answers1

3

It's a bad practice, you shouldn't do anything similar in a controller. The Laravel way is to create global helper in case if you'll use it a lot:

if (! function_exists('convertChars')) {
    function convertChars()
    {
        $result = ....
        ....
        return $result;
    }
}

Add it to a custom helpers.php and autoload it by putting this into composer.json which is in Laravel root directory:

"autoload": {
    ....
    "files": [
        "app/someHelpersDirectory/helpers.php"
    ]
},

Also, since you're trying to convert non-latin characters to latin ones, I guess this method from my package will help you to create more concise and readable helper.

Community
  • 1
  • 1
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279