如何在Laravel而不是Facade上创建Helper方法 [英] How to create Helper Methods on Laravel not a Facade

查看:67
本文介绍了如何在Laravel而不是Facade上创建Helper方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多有关如何在Laravel 5.1中制作辅助方法的问题.但是我不想通过Facade来实现.

I've read a lot of questions on how to make helper methods in Laravel 5.1. But I don't want to achieve this via a Facade.

HelperClass::methodName();

我想像这些方法一样创建辅助方法 Laravel辅助方法喜欢:

I want to make helper methods just like on these methods Laravel Helper Methods like:

myCustomMethod();

我不想使其成为外观.这可能吗?如何?

I don't want to make it a Facade. Is this possible? How?

推荐答案

如果要采用"Laravel方式",则可以使用自定义助手创建helpers.php文件:

If you want to go the 'Laravel way', you can create helpers.php file with custom helpers:

if (! function_exists('myCustomHelper')) {
    function myCustomHelper()
    {
        return 'Hey, it\'s working!';
    }
}

然后将该文件放在某个目录中,将此目录添加到应用程序composer.json的自动加载部分:

Then put this file in some directory, add this directory to autoload section of an app's composer.json:

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

运行composer dumpauto命令,您的助手将像Laravel一样在所有应用程序中正常工作.

Run composer dumpauto command and your helpers will work through all the app, like Laravel ones.

如果您想要更多示例,请在/vendor/laravel/framework/Illuminate/Support/helpers.php

If you want more examples, look at original Laravel helpers at /vendor/laravel/framework/Illuminate/Support/helpers.php

这篇关于如何在Laravel而不是Facade上创建Helper方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆