【问题标题】:Adding Laravel dependency in non-class helper files在非类帮助文件中添加 Laravel 依赖项
【发布时间】:2020-10-10 09:05:11
【问题描述】:

我最近将项目中的 Laravel 版本升级到了 6.x。

现在我知道 helpers 类已从 Laravel 6.0 版本中删除。

但无论如何我需要保留 [root-dir]/helpers.php 文件,它是面向函数的、包含通用帮助函数的非类文件。

在该文件中,我需要将所有以str_ 开头的自定义函数(如str_contains)替换为Illumimnate\Support\Str 对应的Str::contains。例如:

if(!function_exists('is_bot'))
{
    /**
     * userAgent is the user-agent header
     * from the request object
     * @param $userAgent
     * @return bool
     */
    function is_bot($userAgent)
    {
        return str_contains($userAgent, config('bot_check'));
    }
}

我该怎么做?

【问题讨论】:

    标签: php laravel helper laravel-6 laravel-upgrade


    【解决方案1】:

    由于有人抱怨他们而移除了助手,但他们将其移到了新包中 https://github.com/laravel/helpers

    链接到 laravel 的拉取请求 https://github.com/laravel/framework/pull/26898

    【讨论】:

      【解决方案2】:

      您只能在类文件中使用namespace 和use。您可以将您的 helpers.php 文件转换为这样的类:

      <?php
      
      namespace App;
      
      use Illuminate\Support\Str;
      
      class Helper
      {
          public static function is_bot($userAgent)
          {
              return Str::contains($userAgent, config('bot_check'));
          }
      }
      

      并使用\App\Helper::is_bot($userAgent) 在您的 Laravel 应用程序中调用 is_bot 函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-18
        • 2020-01-03
        • 1970-01-01
        • 2020-11-29
        • 2021-01-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多