douchenchepan6465 2019-05-11 19:17
浏览 71

如何在Laravel中使用标准的php包? 我尝试导入它但无法找到它?

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. It's composer.json is located here if look into it is needed: https://github.com/justinrainbow/json-schema/blob/master/composer.json

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.
";
} else {
    echo "JSON does not validate. Violations:
";
    foreach ($validator->getErrors() as $error) {
        echo sprintf("[%s] %s
", $error['property'], $error['message']);
    }
}
  • 写回答

1条回答 默认 最新

  • douzhao5656 2019-05-11 19:27
    关注

    Got it working by doing this:

    use JsonSchema\Validator as JsonValidator;
    

    and in the method:

    $validator = new JsonValidator;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)