Class 在 laravel 中找不到路径

Class path is not found in laravel

我编写这段代码是为了分离 "attractions" 的 class 定义,以便我可以在别处重用它。我会从多个页面需要它。

所以不管我做什么,都找不到零件。

这是class的定义,也可以看到文件夹结构

<?php 
//this Class contains all the attractions.
// It extends the place class

use DB;

class attractions{
    public $json;
    public $name='test';
    public $id;
    public $lat;
    public $lon;
    public $time;
    public $photos;

   public function __construct($id){
  }
}

正在尝试创建类型为 class

的对象 $task
<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;

class AttractionsController extends Controller{    
    public function show($id){
        $task=new attractions($id);
        return view ('pages.attraction',['name'=>$task->name]);
// This is the old one. Can remove after you succeed in the first.         
/*      return view('pages.attraction',[
        'name'=>$task->name
        ]);*/
    }
}

这是弹出的错误 “Symfony\Component\Debug\Exception\FatalThrowableError (E_ERROR) Class 'App\MyClasses\attractions\attractions' 未找到

我尝试了所有可能的路径,但这是不可接受的..

有什么帮助吗? (我知道这可能是一个非常愚蠢和微不足道的错误!)

命名空间添加到您的文件attractions.php

<?php

namespace App\MyClasses;

class attractions {
    public $name = 'test';
    // other variable here ....


    public function example()
    {
        echo "example here";
    }

    public static function test()
    {
        echo "test here";
    }
}

现在转到您的终端并输入此命令

作曲家转储自动加载 这将刷新您的自动加载文件,以便您新创建的 attractions.php 也将被自动加载。

命令执行完毕后,您现在可以像这样使用全局 class。

    use App\MyClasses\attractions;

    SomeClass {

    function __construct()
    {
        // instance call
        $attraction = new attractions();
        $attraction->example();

        // or static call

        attractions::test();
    }
 }

希望对您有所帮助,请告诉我。