我想创建帮助函数,以避免在Laravel视图之间重复代码。例如:

view.blade.php

<p>Foo Formated text: {{ fooFormatText($text) }}</p>

它们基本上是文本格式化函数。我应该如何定义全局可用的帮助函数,如fooFormatText()?


当前回答

我最初的想法是作曲家的自动加载,但它对我来说不太像Laravel 5。L5大量使用了服务提供者,它们引导你的应用程序。

首先,我在我的应用程序目录中创建了一个名为Helpers的文件夹。然后在Helpers文件夹中,我为我想添加的功能添加了文件。在一个文件夹中放置多个文件可以避免一个大文件变得太长而难以管理。

接下来,我通过运行artisan命令创建了一个HelperServiceProvider.php:

artisan make:provider HelperServiceProvider

在register方法中,我添加了这个代码片段

public function register()
{
    foreach (glob(app_path().'/Helpers/*.php') as $filename){
        require_once($filename);
    }
}

最后,在config/app.php的providers数组中注册服务提供者

'providers' => [
    'App\Providers\HelperServiceProvider',
]

现在,您的Helpers目录中的任何文件都已加载,并准备使用。

更新2016-02-22

There are a lot of good options here, but if my answer works for you, I went ahead and made a package for including helpers this way. You can either use the package for inspiration or feel free to download it with Composer as well. It has some built in helpers that I use often (but which are all inactive by default) and allows you to make your own custom helpers with a simple Artisan generator. It also addresses the suggestion one responder had of using a mapper and allows you to explicitly define the custom helpers to load, or by default, automatically load all PHP files in your helper directory. Feedback and PRs are much appreciated!

composer require browner12/helpers

Github:布朗纳12/助手

其他回答

在你的app文件夹中创建一个helpers.php文件,并用composer加载它:

"autoload": {
    "classmap": [
        ...
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        "app/helpers.php" // <---- ADD THIS
    ]
},

然后把它添加到你的作曲家。Json文件,执行如下命令:

composer dump-autoload

如果您不喜欢将helpers.php文件保存在app目录中(因为它不是一个PSR-4命名空间类文件),您可以做laravel.com网站所做的事情:将helpers.php存储在bootstrap目录中。记得在你的作曲器中设置它。json文件:

"files": [
    "bootstrap/helpers.php"
]

在SO和谷歌上筛选了各种各样的答案后,我仍然没有找到一个最佳的方法。大多数答案建议我们离开应用程序,依靠第三方工具Composer来完成工作,但我不相信仅仅为了包含一个文件而耦合到一个工具是明智的。

Andrew Brown的回答最接近我认为应该如何处理它,但是(至少在5.1中),服务提供者步骤是不必要的。Heisian的回答强调了PSR-4的使用,这让我们更接近了一步。下面是视图中helper的最终实现:

首先,在你的app目录中创建一个helper文件,命名空间为:

namespace App\Helpers;

class BobFinder
{
    static function bob()
    {
        return '<strong>Bob?! Is that you?!</strong>';
    }
}

接下来,在config\app.php中,在aliases数组中为你的类添加别名:

'aliases' => [
    // Other aliases
    'BobFinder' => App\Helpers\BobFinder::class
]

这就是你需要做的。PSR-4和别名应该向你的视图公开helper,所以在你的视图中,如果你输入:

{!! BobFinder::bob() !!}

它应该输出:

<strong>Bob?! Is that you?!</strong>

在Laravel 5.3及以上版本中,Laravel团队将所有过程文件(routes.php)移出了app/目录,整个app/文件夹是psr-4自动加载的。接受的答案在这种情况下是可行的,但我感觉不对。

所以我所做的就是在我的项目的根目录下创建了一个helpers/目录,并把helper文件放在里面,放在我的composer中。我这样做:

...
"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        "helpers/ui_helpers.php"
    ]
},
...

这样我的app/目录仍然是psr-4自动加载的,并且帮助程序组织得更好一些。

在dir bootstrap\autoload.php中

require __DIR__.'/../vendor/autoload.php';
require __DIR__.'/../app/Helpers/function.php'; //add

添加这个文件

app\Helpers\function.php

编写自定义helper的最佳实践是

1)在项目根目录的app目录中,创建一个名为Helpers的文件夹(只是为了分离和构造代码)。

2)在文件夹中写入psr-4文件或普通php文件

如果PHP文件的格式是psr-4,那么它将被自动加载,否则在编写器中添加以下行。Json,在项目根目录中

在autoload key中,创建一个名为files的新key来在自动加载时加载文件,在files对象中添加从app目录开始的路径。这里有一个例子。

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        "app/Helpers/customHelpers.php"
    ]
},
"autoload-dev": {
    "classmap": [
        "tests/TestCase.php"
    ]
},

PS:如果文件没有加载,尝试运行composer dump-autoload。