ringord
ringord

Reputation: 958

include class with autoload.php but getting class not found error

I tried to use jasonmapper just as written in manual. I required autoload.php file, and when construct JasonMapper object, I go class not found exception.

(1/1) FatalThrowableError
Class 'App\Http\Controllers\JsonMapper' not found

Here is my code

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

Upvotes: 1

Views: 592

Answers (1)

Brian Gottier
Brian Gottier

Reputation: 4582

If you don't "use" JsonMapper at the top of your script, PHP assumes that JsonMapper is in the App\Http\Controllers namespace, which it's not. That means in your script you must:

$mapper = new \JsonMapper();

Upvotes: 2

Related Questions