我在app/Helpers
中有一些帮助类。如何使用service provider
加载这些类以在刀片模板中使用它们?Laravel 5.4中的自定义帮助类
例如如果我有一个类CustomHelper
包含方法fooBar()
:
<?php
nampespace App\Helpers;
class CustomHelper
{
static function fooBar()
{
return 'it works!';
}
}
我希望能够做我的刀模板是这样的:
{{ fooBar() }}
,而不是这样做:
{{ \App\Helpers\CustomHelper::fooBar() }}
PS: @ andrew-brown's answer in Best practices for custom helpers on Laravel 5 deals wi th非类文件。有一个基于类的解决方案将是很好的,这样辅助函数就可以在类中组织起来。
HTTPS:/ /laravel.io/forum/02-03-2015-best-practices-for-custom-helpers-on-laravel-5?page=1#reply-20564 – hassan
也http://stackoverflow.com/a/32772686/ 2359679 – hassan
你想实现一个基于类的解决方案,但使用它像一个全局函数?不要以为这是可能的任何语言,更不用说PHP或laravel – Paras