我想创建帮助函数,以避免在Laravel视图之间重复代码。例如:
view.blade.php
<p>Foo Formated text: {{ fooFormatText($text) }}</p>
它们基本上是文本格式化函数。我应该如何定义全局可用的帮助函数,如fooFormatText()?
我想创建帮助函数,以避免在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"
]
我最初的想法是作曲家的自动加载,但它对我来说不太像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/助手
Laravel 5中的自定义类,简单的方法
这个答案适用于Laravel中的一般自定义类。有关更多Blade的具体答案,请参阅Laravel 5中的自定义Blade指令。
步骤1:创建您的Helpers(或其他自定义类)文件,并给它一个匹配的名称空间。编写你的类和方法:
<?php // Code within app\Helpers\Helper.php
namespace App\Helpers;
class Helper
{
public static function shout(string $string)
{
return strtoupper($string);
}
}
步骤2:创建别名:
<?php // Code within config/app.php
'aliases' => [
...
'Helper' => App\Helpers\Helper::class,
...
步骤3:在项目根目录下运行composer dump-autoload
第四步:在你的Blade模板中使用它:
<!-- Code within resources/views/template.blade.php -->
{!! Helper::shout('this is how to use autoloading correctly!!') !!}
额外学分:在你的Laravel应用程序的任何地方使用这个类:
<?php // Code within app/Http/Controllers/SomeController.php
namespace App\Http\Controllers;
use Helper;
class SomeController extends Controller
{
public function __construct()
{
Helper::shout('now i\'m using my helper class in a controller!!');
}
...
来源:http://www.php-fig.org/psr/psr-4/
为什么它可以工作:https://github.com/laravel/framework/blob/master/src/Illuminate/Support/ClassLoader.php
自动装填的来源: http://php.net/manual/en/language.oop5.autoload.php
我知道现在回答这个问题已经很晚了,但是,这个问题每天都发生在所有初级开发人员身上,所以对于直接步骤,请执行以下步骤:
**将你的helper函数分组到类中(使用刀片中的函数只是使它们静态),并将所有类放在配置Laravel文件夹app.php别名
'aliases' => [
"YourClassName" => App\Support\YourClassName,
]
现在你可以在刀片和控制器上使用所有的静态函数了。
在dir bootstrap\autoload.php中
require __DIR__.'/../vendor/autoload.php';
require __DIR__.'/../app/Helpers/function.php'; //add
添加这个文件
app\Helpers\function.php