Laravel 没有自动加载助手

Laravel loading helper not automatically

我尝试在没有 composer autoload 的情况下加载我的助手

对于我使用的控制器:

use \App\Helper;

效果很好,但是对于 blade 的视图我该如何加载它?

尝试清除缓存

php artisan cache:clear php artisan config:clear php artisan route:clear

在 blade 视图中可以用作 \App\Helper::call();

@php
    $var = \App\Helper::call();
@endphp

你可以通过两种方式做到这一点

解决方案 1:创建别名

config\app.php 中将 aliases 更改为

'aliases' => [
    'App' => Illuminate\Support\Facades\App::class,
    'Artisan' => Illuminate\Support\Facades\Artisan::class,
    'Auth' => Illuminate\Support\Facades\Auth::class,
    ...................
    'Helper' => App\Helper::class,
]

在blade中使用

@php
    $result = Helper::staticFunction();
    // or
    $helper = app(Helper::class);
    $helper->functionName(); 
@endphp

解决方案 2:

@php
    $result = \App\Helper::staticFunction();
    // ot
    $helper = app(\App\Helper::class);
    $helper->functionName();
@endphp