简体   繁体   English

laravel 5中找不到助手类

[英]helper class not found in laravel 5

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() . 然后添加了'Helper'=> app_path()。 '\\Helpers\\AmcHelper' to aliases array in config/app.php file. '\\ 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

you have written user Helper instead 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');
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM