这不是外墙的工作方式。扩展Illuminate\Support\Facades\Facade需要自定义Facade,基本上只需要自定义Facade实现protected static function getFacadeAccessor()即可。此方法应该返回应该由外观解析的名称(或类或接口)。
外观允许您以静态方式调用实例方法(即非static 方法)。这是可行的,因为门面知道如何将对静态方法的调用重定向到门面后面的实例。这是通过实现__callStatic($method, $args) 来完成的,它只是将静态方法调用重定向到名称由getFacadeAccessor() 返回的实现。
假设您在服务容器中以名称helper 注册了一个服务。然后,您可以使用 app('helper')->getColor() 或 app()->make('helper')->getColor() 在其上执行方法 getColor()。
使用名为Helper 的外观,它通过从getFacadeAccessor() 方法将helper 作为字符串返回来解析您的getFacadeAccessor(),然后您可以使用Helper::getColor() 执行相同的操作。
在你的情况下,你现在有几个选择:
1) 使用具有静态方法的类:
与您已经做过的类似,您可以使用静态方法定义一个类。然后,您可以使用完全限定的类名 (FQCN) 从刀片视图中静态调用这些方法:
// app/Helpers/Helper.php
class Helper
{
public static function getColor(): string
{
return 'blue';
}
}
// resources/views/some/page.blade.php
<div style="color:{{ \App\Helpers\Helper::getColor() }}"> ... </div>
2) 使用带有外观的非静态类:
您可以使用与上述类似的类和非静态方法并为其添加外观:
// app/Helpers/Helper.php
class Helper
{
public function getColor(): string
{
return 'blue';
}
}
// app/Facades/Helper.php
class Helper extends \Illuminate\Support\Facades\Facade
{
protected static function getFacadeAccessor()
{
return \App\Helpers\Helper::class;
}
}
// config/app.php -> 'aliases' array
[
// ... other facades ...
'Helper' => \App\Facades\Helper::class,
]
// resources/views/some/page.blade.php
<div style="color:{{ \Helper::getColor() }}"> ... </div>
3) 使用全局非类帮助文件:
您还可以定义一个基本的 PHP 文件,其中包含一些全局注册的辅助函数。这些函数不是类方法,因此不需要使用类前缀调用:
// app/Helpers/color_utils.php
if (!function_exists('get_color')) {
function get_color()
{
return 'blue';
}
}
// app/Providers/HelperServiceProvider.php
class HelperServiceProvider extends \Illuminate\Support\ServiceProvider
{
public function register(): void
{
$filenames = glob(app_path('Helpers/*.php'));
if ($filenames !== false && is_iterable($filenames)) {
foreach ($filenames as $filename) {
require_once $filename;
}
}
}
}
// config/app.php -> 'providers' array
[
// ... other providers ...
\App\Providers\HelperServiceProvider::class,
]
// resources/views/some/page.blade.php
<div style="color:{{ get_color() }}"> ... </div>
4) 使用类和服务注入:
另外一个不错的选择是使用服务容器将服务注入到 Blade 模板中。 Laravel 为其提供了一个名为 @inject($var, $fqdn) 的 Blade 指令。
// app/Helpers/Helper.php
class Helper
{
public static function getColor(): string
{
return 'blue';
}
}
// resources/views/some/page.blade.php
@inject('helper', \App\Helpers\Helper::class)
<div style="color:{{ $helper->getColor() }}"> ... </div>
我希望代码不言自明。文件的命名空间是故意省略的,当然你应该根据目录使用命名空间(符合PSR-4)。
如果您不需要任何依赖项并且您基本上只需要静态访问某些东西,我个人更喜欢全局帮助器(选项 3)。