RESTful Api using Zend framework 2

a Small guide on creating a simple RESTful api using Zend framework 2 (further zf2).


We need zf2 v2.3@dev and Doctrine 2 ORM.

/ > Let's start with creating the directory structure of our api:

the
mkdir -pv zf2-api/{config/autoload,public,module/v1/{config,src/v1/{Controller,Service,Entities}}}


Download composer
the
curl -sS https://getcomposer.org/installer | php

So we need to create composer.json with these dependencies and install them.
the
"require": {
"php": "> =5.4",
"zendframework/zendframework": "2.*@dev",
"doctrine/doctrine-orm-module" :"0.*"
}


Entry point public/index.php we will copy from the zf2 skeleton appliaction:

the
<?php

chdir(dirname(__DIR__));

// So we  take  the initialization autoloader
require 'init_autoloader.php';

define('BASE_DIR', dirname(__DIR__));

// Run the application!
Zend\Mvc\Application::init(require 'config/application.config.php')->run();


Create config application.config.php to run our app, our module will be called respectively v1 version of the api:

the
return array(
// Zagrozenie modules
'modules' => array(
'v1',
'DoctrineModule',
'DoctrineORMModule',
),
// Setting "for ModuleManager listeners
'module_listener_options' => array(
'module_paths' => array(
'./module',
'./vendor',
),
// Adjust the paths for both global and local configs
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
),
// Include the test module dependencies
'check_dependencies' => true,
),
);


Now let's create the main class of our module module/v1/Module.php:

the
<?php
/**
* Description of Module
*
* @author cawa
*/

namespace v1;

class Module
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}

public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ = > __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}


And a simple config for it module/v1/config/module.config.php:

the
<?php

/**
* Config module v1
*
* @author cawa
*/

namespace v1;

return array(
'router' => array(
'routes' => array(
'api' => array(
'type' => 'Segment',
'options' => array(
'route' => '/api/v1/[:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*/?',
),
'defaults' => array(
'__NAMESPACE__' => 'v1\Controller',
'controller' => 'v1\Controller\Index',
'action' => 'index'
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'v1\Controller\Index' => 'v1\Controller\IndexController',
),
),
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy',
),
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5'
),
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' = > 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
)
)
),
),
// Placeholder for console routes
'console' => array(
'router' => array(
'routes' => array(
),
),
),
);


And our only controller module/v1/src/v1/Controller/IndexController:

the
<?php
/*
Document : IndexController
Created on : 28.10.2013, 11:37:11
Author : cawa
Description:
Index controller
*/

namespace v1\Controller;

use Zend\Mvc\Controller\AbstractRestfulController,
Zend\View\Model\JsonModel;

class IndexController extends AbstractRestfulController
{

public function indexAction()
{
$em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
$testEntity = $em->getRepository('v1\Entity\Test')->findAll();

//yeah not a good solution, but just to show fit
foreach ($testEntity as $entity) {
$array[] = $entity->getJsonData();

}
return new JsonModel(array('response' => $array));
}

}


Next, start the cli server php -S localhost:8000 from the public directory, and follow the link localhost:8000/api/v1/index, we get the response from the server.

PS
github: zf2-api
If you have any questions, write comments, happy to answer.
If anyone is interested, I can continue with more detailed guide.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Integration of PostgreSQL with MS SQL Server for those who want faster and deeper

Custom database queries in MODx Revolution

Google Web Mercator: a mixed coordinate system