alterway/rest-problem-bundle

遵循 HTTP APIs RFC (http://tools.ietf.org/html/draft-nottingham-http-problem-03) 的 Problem Details

安装: 628

依赖项: 0

建议者: 0

安全: 0

星标: 5

关注者: 14

分支: 7

开放问题: 0

类型:symfony-bundle

v0.0.1 2013-10-30 15:24 UTC

This package is not auto-updated.

Last update: 2024-09-24 04:33:29 UTC


README

# RestProblemBundle

用于管理 API 中问题的 Symfony2 扩展包。

Build Status

此扩展包遵循 HTTP APIs 的 Problem Details 建议。

## 安装

编辑你的 composer.json

"require": {
    "alterway/rest-problem-bundle" : "master"
}

并运行 Composer

php composer.phar update alterway/rest-problem-bundle

AppKernel.php 中启用你的扩展包

// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Alterway\Bundle\RestProblemBundle\AwRestProblemBundle(),
    );
}

使用方法

use Alterway\Bundle\RestProblemBundle\Response\ProblemResponse;
use Alterway\Bundle\RestProblemBundle\Problem;

public function demoAction(Request $request)
{

    $form = // (...)

    $form->bind($request);
    if (!$form->isValid()) {
        $problem = new Problem\InvalidQueryForm($form);
        return new ProblemResponse($problem, 403);
    }
}

## 使用注解

请记住启用注解

sensio_framework_extra:
router:  { annotations: true }
request: { converters: true }
view:    { annotations: true }
cache:   { annotations: true }

并在你的 app/autoload.php 文件中注册自动加载器

Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(array($loader, 'loadClass')); 

这将发送一个 application/api-problem+json 头部

use Alterway\Bundle\RestProblemBundle\Response\ProblemResponse;
use Alterway\Bundle\RestProblemBundle\Controller\Annotations\Problem;

/**
* @Problem
*/
public function demoAction(Request $request)
{

    $form = // (...)

    $form->bind($request);
    if (!$form->isValid()) {
        return new Problem\InvalidQueryForm($form);
    }
}

问题类型

你需要创建自己的问题类型。默认有以下问题类型

  • Problem\InvalidQuery
  • Problem\InvalidQueryForm
  • ...

Todo

添加控制器和路由以提供 problemType

## 贡献

安装开发依赖项

php composer.phar update --dev

运行 Behat

./vendor/bin/behat @AwRestProblemBundle

版权

版权 (c) 2013 La Ruche Qui Dit Oui!。详细信息请见 LICENSE。

贡献者

  • 负责人:Jean-François Lépine (Halleck45)
  • 开发者:Antoine Lévêque (gh0stonio)

赞助商