qi/qapi

There is no license information available for the latest version (1.0.0-beta.1) of this package.

RESTful API module for Yii Framework

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

This package is not auto-updated.

Last update: 2024-04-23 04:48:16 UTC


README

Yii Framework

Install

Set path aliases in /protected/config/main.php and /protected/config/console.php:

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

Set the module and its components:

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

Set urlManages rules:

<?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')
		),
	),
	...
),
...
?>

Create DB tables:

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

Configuration

Default format

In case the request doesn't have a "Accept" header or it's set to "/", response's "Content-Type" will be set to "application/xml" and XML parser will be used. To change default API format set module's defaultFormat property.

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

Controllers

Mapping

Use module's controllerPath or controllerMap properties to configure API resources.

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

Note, that controllerMap has higher priority than controllerPath, so in case you want to override module's default controllers (default, auth, settings), you should set it with controllerMap.