Home

Awesome

APIx, RESTful services for PHP Build Status

Latest Stable Version Build Status Code Quality Code Coverage License

APIx is a (micro-)framework to build RESTful Web services. It will run alognside your existing framework/application with minimum fuss.

Some of its features:

Todo:

Feel free to comment, send pull requests and patches...

Basic usage

<?php
    require 'apix.phar';

    // Instantiate the server (using the default config)
    $api = new Apix\Server;

    // Create a GET handler $name is required
    $api->onRead('/hello/:name', function($name) {
        return array('Hello ' . $name);
    });

    $api->run();

Another example using annotations.

    // $type and $stuff are required parameters.
    // $optional is not mandatory.
    $api->onRead('/search/:type/with/:stuff/:optional',
        /**
         * Search for things by type that have stuff.
         *
         * @param     string  $type         A type of thing to search upon
         * @param     string  $stuff        One or many stuff to filter against
         * @param     string  $optional     An optional field
         * @return    array
         * @api_auth  groups=clients,employes,admins users=franck,jon
         * @api_cache ttl=12mins tags=searches,indexes
         */
        function($type, $stuff, $optional = null) {
            // some logic
            return $results;
        }
    );

    $api->run();

Advanced usage

Routing

A route defines the path to a resource, once matched the corresponding resource's controller and dedicated handlers are invoked.

Any returned value emanating from a resource's controller, generally an associative array, will become the main subject of the response.

Essentially, a route is made of:

  1. A route controller that corresponds to a HTTP header method:
<pre> onCreate() -> POST | onModify() -> PATCH onRead() -> GET | onHelp() -> OPTIONS onUpdate() -> PUT | onTest() -> HEAD onDelete() -> DELETE | onTrace() -> TRACE </pre>
  1. A route path corresponding to a Request-URI.
    • It may represent a specific and static resource entity, such as: <pre>/search/france/paris</pre>
    • It may also be dynamic, and may include one or many variables indicated by a colon :, such as: <pre>/search/:country/:city</pre>

Controller definitions

A resource controller may be declared as either:

It will use:

Here is an example showing these in context:

    $api->onRead('/category/:name', function(Request $request, $name) {

        // retrieve a named param
        $page = (int) $request->getParam('page');

        // retrieve the body params, parsed from XML, JSON, ...
        $params = $request->getBodyParams();

        ...

        return $list_defs;
    });

Configuration

Check the inline comments in the config.dist.php file shipped with the distribution.

Bootstrap

To boostrap an Apix server, add the required file and create an instance of the Apix\Server.

A dedicated configuration file can be injected to an Apix server:

    <?php
        require 'apix.phar';

        $api = new Apix\Server(require 'my_config.php');

        $api->run();

PHAR Console

Apix PHAR distribution contains a built-in console. Try invoking the api.phar file on the command line as follow:

$ php apix.phar --help

Web server configuration

Use one of the vhost file provided within the distribution and follow the relevant instructions provided in the comments to set your web server environement.

TODO: Add ngynx and lighttpd files to the distrib.

Annotations

Annotations can be used to define many aspects of a resource entity.

Here is a self explanatory example:

    <?php
        $api->onRead('/download/:app/version/:version',
            /**
             * Retrieve the named sotfware
             * Anyone can use this resource entity to download apps. If no
             * version is specified the latest revision will be returned.
             *
             * @param     string    $app        The name of the app
             * @param     string    $version    The version number.
             * @return    array     A response array.
             *
             * @api_auth  groups=public
             * @api_cache ttl=1week tags=downloads
             */
            function($app, $version=null) {
                // ...
                return array(
                    $app => 'the version string of software.'
                );
            }
        );

        $api->onCreate('/upload/:software',
            /**
             * Upload a new software
             * Admin users use this resource entity to upload new software.
             *
             * @param      Request  $request   The Server Request object.
             * @param      string   $software
             * @return     array    A response array.
             *
             * @api_auth   groups=admin users=franck
             * @api_cache  purge=downloads
             */
            function(Request $request, $software) {
                // ...
            }
        );

        $api->run();

Installation

Apix requires PHP 5.3 or later.

    <dependencies>
      <required>
        <package>
          <name>apix_server</name>
          <channel>pear.ouarz.net</channel>
          <min>1.0.0</min>
          <max>1.999.9999</max>
        </package>
      </required>
    </dependencies>
    sudo pear channel-discover pear.ouarz.net
    sudo pear install --alldeps ouarz/apix

For more details see pear.ouarz.net.

Testing

To run the unit test suite simply run phpunit from within the main dir.

Integration and functional tests are also available in the src/tests.

License

APIx is licensed under the New BSD license -- see the LICENSE.txt for the full license details.

<pre> _|_| _|_| _| _| _| _| _| _| _| _| _| _| _| _| _| _| _|_| _|_|_|_| _|_|_| _| _|_| _|_| _| _| _| _| _| _| _| _| _| _| _| _| </pre>