📜 ⬆️ ⬇️

RESTful Api using Zend framework 2

A small guide to creating the simplest RESTful api using Zend framework 2 (hereinafter zf2).


We need zf2 v2.3@dev, as well as Doctrine 2 ORM.

So let's start by creating the directory structure of our api:

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

')
Load the composer
 curl -sS https://getcomposer.org/installer | php 

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


We will copy the entry point public / index.php completely with zf2 skeleton appliaction:

 <?php chdir(dirname(__DIR__)); //       require 'init_autoloader.php'; define('BASE_DIR', dirname(__DIR__)); // Run the application! Zend\Mvc\Application::init(require 'config/application.config.php')->run(); 


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

 return array( //   'modules' => array( 'v1', 'DoctrineModule', 'DoctrineORMModule', ), //  "  ModuleManager 'module_listener_options' => array( 'module_paths' => array( './module', './vendor', ), //        'config_glob_paths' => array( 'config/autoload/{,*.}{global,local}.php', ), //     'check_dependencies' => true, ), ); 


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

 <?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__, ), ), ); } } 


As well as the simplest config for it module / v1 / config / module.config.php:

 <?php /** *   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:

 <?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(); //    ,     foreach ($testEntity as $entity) { $array[] = $entity->getJsonData(); } return new JsonModel(array('response' => $array)); } } 


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

PS
github: zf2-api
If you have any questions, write comments, I will answer with pleasure.
If someone is interested, I can continue with a more detailed guide.

Source: https://habr.com/ru/post/199502/


All Articles