Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nikocraft's avatar

How to use standard php package in Laravel? I try to import it but it can't be found?

I installed this Json Verification package:

https://github.com/justinrainbow/json-schema

It's just a PHP package it installs into vendor/justinrainbow/json-schema. I need to access it in my Code so I tried this in my Controller:

    use JsonSchema\Validator;

when I do this inside a method:

    $validator = new JsonSchema\Validator;

I get this error:

     Symfony\Component\Debug\Exception\FatalThrowableError  : Class 'App\Commands\JsonSchema\Validator' not found

Package has been installed with composer and is in the vendor folder.

How do I access it so that I can run some code that it shows on its GitHub page:

    $data = json_decode(file_get_contents('data.json'));
    
    // Validate
    $validator = new JsonSchema\Validator;
    $validator->validate($data, (object)['$ref' => 'file://' . realpath('schema.json')]);
    
    if ($validator->isValid()) {
        echo "The supplied JSON validates against the schema.\n";
    } else {
        echo "JSON does not validate. Violations:\n";
        foreach ($validator->getErrors() as $error) {
            echo sprintf("[%s] %s\n", $error['property'], $error['message']);
        }
    }
0 likes
3 replies
jlrdw's avatar

Have you installed it with composer.

MThomas's avatar

Once you imported the method using the use statement at the top of your file, you don’t need to use the whole paths of your class, just do

$validator = new Validator;

Or add a slash before the path, otherwise it looks relative to your current namespace (this is the case within the class, not for the use statement)

$validator = new \JsonScheme\Validator;

Sorry for bad markup, am on my iPhone

1 like
nikocraft's avatar

This also worked:

use JsonSchema\Validator as JsonValidator;

$validator = new JsonValidator;

Please or to participate in this conversation.