在 Laravel 5 中找不到助手类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29274538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:13:48  来源:igfitidea点击:

helper class not found in laravel 5

phplaravellaravel-4laravel-5

提问by Ayman Hussein

I have create Helpers folder inside app, then I have created php file amchelpers.php ---> app/Helpers/amchelpers.php

我在应用程序中创建了 Helpers 文件夹,然后我创建了 php 文件 amchelpers.php ---> app/Helpers/amchelpers.php

amchelpers.php code:

amchelpers.php 代码:

<?php namespace App;

   class AmcHelper {
      static function displayString($string){
          return $string;
      }
  }

then added these lines to composer.json

然后将这些行添加到 composer.json

"files": [
       "app/Helpers/amchelpers.php"
    ]

then run this command:

然后运行这个命令:

composer dump-autoload

then added 'Helper' => app_path() . '\Helpers\AmcHelper' to aliases array in config/app.php file.

然后添加 'Helper' => app_path() 。'\Helpers\AmcHelper' 到 config/app.php 文件中的别名数组。

in my controller I have below action (this action defined in route.php):

在我的控制器中,我有以下操作(此操作在 route.php 中定义):

use Helper;

class UserController extends Controller {
   public function displayMyString(){  
         echo Helper::displayString('Hello');
   }
}

when run the page http://localhost:8080/easy_marketing/public/displayMyString

运行页面时http://localhost:8080/easy_marketing/public/displayMyString

I Got:

我有:

ErrorException in compiled.php line 6367: Class 'C:\wamp\www\easy_marketing\app\Helpers\AmcHelper' not found

回答by Anand Patel

you have written user Helperinstead of use Helper

你写了user Helper而不是use Helper

or

或者

another way to achieve this is

实现这一目标的另一种方法是

Laravel 5 App directory is autoloaded by default with its folder, what you have to take care is add namespace followed by directory name,

Laravel 5 App 目录默认自动加载其文件夹,你需要注意的是添加命名空间,然后是目录名,

so directory structure is App --> Helpers

所以目录结构是 App --> Helpers

so your name space must include App\Helpers

所以你的名字空间必须包含 App\Helpers

try following code

尝试以下代码

<?php namespace App\Helpers;

   class AmcHelper {
      static function displayString($string){
          return $string;
      }
  }

and when you are using this class in another class write this after namespace declaration

当你在另一个类中使用这个类时,在命名空间声明之后写这个

use App\Helpers\AmcHelper as Helper;


class UserController extends Controller {
   public function displayMyString(){  
         echo Helper::displayString('Hello');
   }
}