qi / qapi

该包最新版本(1.0.0-beta.1)没有可用的许可证信息。

Yii 框架的 RESTful API 模块

维护者

详细信息

gitlab.com/qi/qapi

源代码

问题

1.0.0-beta.1 2014-07-02 12:06 UTC

This package is not auto-updated.

Last update: 2024-09-24 06:38:02 UTC


README

Yii 框架

安装

在 /protected/config/main.php 和 /protected/config/console.php 中设置路径别名

<?php
	Yii::setPathOfAlias('api', dirname(dirname(__FILE__)) . '/vendor/qi/qApi');
	...
?>

设置模块及其组件

<?php
...
'modules' => array(
	...
    'api' => array(
        'class' => 'api.ApiModule',
        'components' => array(
            'xform' => array('class' => 'qApiParserXForm'),
            'json' => array('class' => 'qApiParserJSON'),
            'xml' => array('class' => 'qApiParserXML'),
        ),
    ),
    ...
),
...
?>

设置 urlManages 规则

<?php
...
'components' => array(
	...
	'urlManager' => array(
		'urlFormat' => 'path',
		'appendParams' => false,
        'showScriptName' => false,
		'rules' => array_merge(
			array(
				// app rules
			),
			include_once(dirname(__FILE__) . '/../vendor/qi/qApi/config/rules_path.php')
		),
	),
	...
),
...
?>

创建数据库表

$ ./protected/yiic migrate --migrationPath=api.migrations --interactive=0

配置

默认格式

如果请求没有 "Accept" 头部或设置为 "/",则响应的 "Content-Type" 将被设置为 "application/xml",并将使用 XML 解析器。要更改默认 API 格式,请设置模块的 defaultFormat 属性。

<?php
	...
	'modules' => array(
		...
        'api' => array(
            'class' => 'api.ApiModule',
            ...
            'defaultFormat' => 'application/json',
            ...
        ),
        ...
    ),
    ...
?>

控制器

映射

使用模块的 controllerPathcontrollerMap 属性来配置 API 资源。

<?php
	...
	'modules' => array(
		...
        'api' => array(
            'class' => 'api.ApiModule',
            ...
            'controllerPath' => dirname(__FILE__) . '/../controllers/api',
            'controllerMap' => array(
                'logs' => 'application.controllers.api.LogsController',
                'settings' => null,
            ),
            ...
        ),
        ...
    ),
    ...
?>

注意,controllerMap 的优先级高于 controllerPath,因此如果您想覆盖模块的默认控制器(default, auth, settings),您应该使用 controllerMap 来设置它。