使用autoload.php包含类,但找不到类错误

时间:2017-08-29 18:53:44

标签: php laravel-5 composer-php

我尝试使用jasonmapper,正如手册中所述。 我需要autoload.php文件,当构造JasonMapper对象时,我会找到class not found异常。

(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);
    }
}

1 个答案:

答案 0 :(得分:2)

如果您没有在脚本的顶部“使用”JsonMapper,PHP会假定JsonMapper位于App \ Http \ Controllers命名空间中,而不是。这意味着在您的脚本中您必须:

$mapper = new \JsonMapper();