使用autoload.php包含类,但获取未找到类错误

使用autoload.php包含类,但获取未找到类错误,php,laravel-5,composer-php,Php,Laravel 5,Composer Php,我试着像手册上写的那样使用。 我需要autoload.php文件,当构造JasonMapper对象时,我去类找不到异常 (1/1) FatalThrowableError Class 'App\Http\Controllers\JsonMapper' not found 这是我的密码 namespace App\Http\Controllers; require __dir__.'/../../../vendor/autoload.php'; use Illuminate\Http\Requ

我试着像手册上写的那样使用。 我需要
autoload.php
文件,当构造
JasonMapper
对象时,我去类找不到异常

(1/1) FatalThrowableError
Class 'App\Http\Controllers\JsonMapper' not found
这是我的密码

namespace App\Http\Controllers;

require __dir__.'/../../../vendor/autoload.php';
use Illuminate\Http\Request;
use App\Http\Games\Numbers;

class ApiController extends Controller
{
    public function home()
    {
        $client = new \GuzzleHttp\Client();
        $res = $client->request(
          'GET',
          $testurl
        );
        $json = json_decode($res->getBody());
        $mapper = new JsonMapper();// error occurs at this line
        $numbers = $mapper->map($json, new Numbers());
        return json_encode($numbers);
    }
}
如果不在脚本顶部“使用”JsonMapper,PHP会假定JsonMapper位于App\Http\Controllers名称空间中,而事实并非如此。这意味着您必须在脚本中:

$mapper = new \JsonMapper();

new\JsonMapper()?可能的副本无需在每个类中要求
供应商/autoload.php
。仅在应用程序的入口点中需要它。只需尝试此操作,就会出现“Class'JsonMapper'notfound”错误。添加\以某种方式解决了原始错误,但仍然找不到JsonMapper。是否通过Composer安装了JsonMapper?如果没有,您可能必须对composer.json进行更改,然后运行composer dump autoload。