1

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

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

2 Comments

Just tried this, and got "Class 'JsonMapper' not found" error. Adding \ somehow resolve the original error but somehow JsonMapper is still not located.
Did you install JsonMapper through Composer? If not, you may have to make changes to your composer.json and then run composer dump-autoload.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.