Laravel 5.4中的自定义帮助程序类

时间:2022-10-20 10:36:57

I have some helper classes in app/Helpers. How do I load these classes using a service provider to use them in blade templates?

我在app / Helpers中有一些辅助类。如何使用服务提供商加载这些类以在刀片模板中使用它们?

e.g. If I have a class CustomHelper that contains a method fooBar() :

例如如果我有一个包含方法fooBar()的CustomHelper类:

<?php

nampespace App\Helpers;

class CustomHelper
{
    static function fooBar()
    {
        return 'it works!';
    }
}

I want to be able to do something like this in my blade templates:

我希望能够在我的刀片模板中执行以下操作:

{{ fooBar() }}

instead of doing this:

而不是这样做:

{{ \App\Helpers\CustomHelper::fooBar() }}

P.S: @andrew-brown's answer in Best practices for custom helpers on Laravel 5 deals with non-class files. It would be nice to have a class based solution so that the helper functions can be organized among classes.

P.S:@ andrew-brown在Laravel 5上的自定义帮助程序的最佳实践中的答案涉及非类文件。拥有一个基于类的解决方案会很好,这样可以在类之间组织帮助函数。

1 个解决方案

#1


31  

I don't think it's possible to use only function when you have code in your classes. Well, you could try with extending Blade but it's too much.

我不认为只有在类中有代码时才能使用函数。好吧,你可以尝试扩展Blade,但它太多了。

What you should do is creating one extra file, for example app\Helpers\helpers.php and in your composer.json file put:

你应该做的是创建一个额外的文件,例如app \ Helpers \ helpers.php,并在你的composer.json文件中放置:

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": ["app/Helpers/helpers.php"] // <- this line was added
},

create app/Helpers/helpers.php file and run

创建app / Helpers / helpers.php文件并运行

composer dump-autoload

Now in your app/Helpers/helpers.php file you could add those custom functions for example like this:

现在在您的app / Helpers / helpers.php文件中,您可以添加这些自定义函数,例如:

if (! function_exists('fooBar')) {
   function fooBar() 
   {
      return \App\Helpers\CustomHelper::fooBar();
   }
}

so you define global functions but in fact all of them might use specific public methods from some classes.

所以你定义了全局函数,但事实上它们都可能使用某些类的特定公共方法。

By the way this is exactly what Laravel does for its own helpers for example:

顺便说一句,这正是Laravel为自己的助手所做的事情,例如:

if (! function_exists('array_add')) {
    function array_add($array, $key, $value)
    {
        return Arr::add($array, $key, $value);
    }
}

as you see array_add is only shorter (or maybe less verbose) way of writing Arr::add

如你所见,array_add只是编写Arr :: add的更短(或者更简洁)的方式

#1


31  

I don't think it's possible to use only function when you have code in your classes. Well, you could try with extending Blade but it's too much.

我不认为只有在类中有代码时才能使用函数。好吧,你可以尝试扩展Blade,但它太多了。

What you should do is creating one extra file, for example app\Helpers\helpers.php and in your composer.json file put:

你应该做的是创建一个额外的文件,例如app \ Helpers \ helpers.php,并在你的composer.json文件中放置:

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": ["app/Helpers/helpers.php"] // <- this line was added
},

create app/Helpers/helpers.php file and run

创建app / Helpers / helpers.php文件并运行

composer dump-autoload

Now in your app/Helpers/helpers.php file you could add those custom functions for example like this:

现在在您的app / Helpers / helpers.php文件中,您可以添加这些自定义函数,例如:

if (! function_exists('fooBar')) {
   function fooBar() 
   {
      return \App\Helpers\CustomHelper::fooBar();
   }
}

so you define global functions but in fact all of them might use specific public methods from some classes.

所以你定义了全局函数,但事实上它们都可能使用某些类的特定公共方法。

By the way this is exactly what Laravel does for its own helpers for example:

顺便说一句,这正是Laravel为自己的助手所做的事情,例如:

if (! function_exists('array_add')) {
    function array_add($array, $key, $value)
    {
        return Arr::add($array, $key, $value);
    }
}

as you see array_add is only shorter (or maybe less verbose) way of writing Arr::add

如你所见,array_add只是编写Arr :: add的更短(或者更简洁)的方式