Best Practices for Custom Helpers in Laravel 5

后端 未结 20 1989
暖寄归人
暖寄归人 2020-11-22 06:40

I would like to create helper functions to avoid repeating code between views in Laravel 5:

view.blade.php

Foo Formated text: {{ fo

20条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 07:27

    This is my HelpersProvider.php file:

    helpers as $helper) {
                $helper_path = app_path().'/Helpers/'.$helper.'.php';
    
                if (\File::isFile($helper_path)) {
                    require_once $helper_path;
                }
            }
        }
    }
    

    You should create a folder called Helpers under the app folder, then create file called whatever.php inside and add the string whatever inside the $helpers array.

    Done!

    Edit

    I'm no longer using this option, I'm currently using composer to load static files like helpers.

    You can add the helpers directly at:

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

提交回复
热议问题