mkdir -pv zf2-api/{config/autoload,public,module/v1/{config,src/v1/{Controller,Service,Entities}}}
curl -sS https://getcomposer.org/installer | php
"require": { "php": ">=5.4", "zendframework/zendframework": "2.*@dev", "doctrine/doctrine-orm-module" :"0.*" }
<?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();
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, ), );
<?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__, ), ), ); } }
<?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( ), ), ), );
<?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)); } }
Source: https://habr.com/ru/post/199502/
All Articles