{"id":199502,"date":"2013-10-29T12:40:03","date_gmt":"2013-10-29T08:40:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=199502"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=199502","title":{"rendered":"<span class=\"post_title\">RESTful Api \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f Zend framework 2<\/span>"},"content":{"rendered":"<div class=\"content html_format\">\n<h4>\u041d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0433\u0430\u0439\u0434 \u043f\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u0433\u043e RESTful api \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Zend framework 2 (\u0434\u0430\u043b\u0435\u0435 zf2).<\/h4>\n<p>  \u041d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f zf2 v2.3@dev, \u0430 \u0442\u0430\u043a \u0436\u0435 Doctrine 2 ORM.<\/p>\n<p>  \u0418\u0442\u0430\u043a, \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0439 \u043d\u0430\u0448\u0435\u0433\u043e api:<\/p>\n<pre><code class=\"bash\">mkdir -pv zf2-api\/{config\/autoload,public,module\/v1\/{config,src\/v1\/{Controller,Service,Entities}}}<\/code><\/pre>\n<p>  \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u043a\u043e\u043c\u043f\u043e\u0437\u0435\u0440   <\/p>\n<pre><code class=\"bash\">curl -sS https:\/\/getcomposer.org\/installer | php<\/code><\/pre>\n<p>  \u0422\u0430\u043a \u0436\u0435 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c composer.json \u0441 \u0442\u0430\u043a\u0438\u043c\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044f\u043c\u0438 \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0438\u0445.  <\/p>\n<pre><code class=\"css\">&quot;require&quot;: {         &quot;php&quot;: &quot;&gt;=5.4&quot;,         &quot;zendframework\/zendframework&quot;: &quot;2.*@dev&quot;,         &quot;doctrine\/doctrine-orm-module&quot; :&quot;0.*&quot;     } <\/code><\/pre>\n<p>  <a name=\"habracut\"><\/a><br \/>  \u0412\u0445\u043e\u0434\u043d\u0443\u044e \u0442\u043e\u0447\u043a\u0443 public\/index.php \u043c\u044b \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u0441 zf2 skeleton appliaction:<\/p>\n<pre><code class=\"php\">&lt;?php  chdir(dirname(__DIR__));  \/\/ \u0422\u0430\u043a \u0436\u0435 \u043c\u044b \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0430\u0432\u0442\u043e\u0437\u0430\u0433\u0440\u0443\u0437\u0447\u0438\u043a\u0430 require 'init_autoloader.php';  define('BASE_DIR', dirname(__DIR__));  \/\/ Run the application! Zend\\Mvc\\Application::init(require 'config\/application.config.php')-&gt;run(); <\/code><\/pre>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0444\u0438\u0433 application.config.php \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043d\u0430\u0448 \u043c\u043e\u0434\u0443\u043b\u044c \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f v1 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u0435\u0440\u0441\u0438\u0438 api:<\/p>\n<pre><code class=\"php\">return array(     \/\/ \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c\u043c\u044b\u0435 \u043c\u043e\u0434\u0443\u043b\u0438     'modules' =&gt; array(         'v1',         'DoctrineModule',         'DoctrineORMModule',     ),     \/\/ \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 &quot;\u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u0435\u0439 \u0434\u043b\u044f ModuleManager     'module_listener_options' =&gt; array(         'module_paths' =&gt; array(             '.\/module',             '.\/vendor',         ),         \/\/ \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u0443\u0442\u0435\u0439 \u0434\u043b\u044f \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0445 \u0438 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u043a\u043e\u043d\u0444\u0438\u0433\u043e\u0432         'config_glob_paths' =&gt; array(             'config\/autoload\/{,*.}{global,local}.php',         ),         \/\/ \u0412\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043c\u043e\u0434\u0443\u043b\u0435\u0439         'check_dependencies' =&gt; true,     ), ); <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043d\u0430\u0448\u0435\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044f module\/v1\/Module.php:<\/p>\n<pre><code class=\"php\">&lt;?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' =&gt; array(                 'namespaces' =&gt; array(                     __NAMESPACE__ =&gt; __DIR__ . '\/src\/' . __NAMESPACE__,                 ),             ),         );     } } <\/code><\/pre>\n<p>  \u0410 \u0442\u0430\u043a \u0436\u0435 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u0439 \u043a\u043e\u043d\u0444\u0438\u0433 \u0434\u043b\u044f \u043d\u0435\u0433\u043e module\/v1\/config\/module.config.php:<\/p>\n<pre><code class=\"php\">&lt;?php  \/**  * \u041a\u043e\u043d\u0444\u0438\u0433 \u043c\u043e\u0434\u0443\u043b\u044f v1  *  * @author cawa  *\/  namespace v1;  return array(     'router' =&gt; array(         'routes' =&gt; array(             'api' =&gt; array(                 'type' =&gt; 'Segment',                 'options' =&gt; array(                     'route' =&gt; '\/api\/v1\/[:action][\/:id]',                     'constraints' =&gt; array(                         'action' =&gt; '[a-zA-Z][a-zA-Z0-9_-]*\/?',                     ),                     'defaults' =&gt; array(                         '__NAMESPACE__' =&gt; 'v1\\Controller',                         'controller' =&gt; 'v1\\Controller\\Index',                         'action' =&gt; 'index'                     ),                 ),             ),         ),     ),     'controllers' =&gt; array(         'invokables' =&gt; array(             'v1\\Controller\\Index' =&gt; 'v1\\Controller\\IndexController',         ),     ),     'view_manager' =&gt; array(         'strategies' =&gt; array(             'ViewJsonStrategy',         ),         'display_not_found_reason' =&gt; true,         'display_exceptions' =&gt; true,         'doctype' =&gt; 'HTML5'     ),     'doctrine' =&gt; array(         'driver' =&gt; array(             __NAMESPACE__ . '_driver' =&gt; array(                 'class' =&gt; 'Doctrine\\ORM\\Mapping\\Driver\\AnnotationDriver',                 'cache' =&gt; 'array',                 'paths' =&gt; array(__DIR__ . '\/..\/src\/' . __NAMESPACE__ . '\/Entity')             ),             'orm_default' =&gt; array(                 'drivers' =&gt; array(                     __NAMESPACE__ . '\\Entity' =&gt; __NAMESPACE__ . '_driver'                 )             )         ),     ),     \/\/ Placeholder for console routes     'console' =&gt; array(         'router' =&gt; array(             'routes' =&gt; array(             ),         ),     ), ); <\/code><\/pre>\n<p>  \u0418 \u043d\u0430\u0448 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 module\/v1\/src\/v1\/Controller\/IndexController:<\/p>\n<pre><code class=\"php\">&lt;?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-&gt;getServiceLocator()-&gt;get('doctrine.entitymanager.orm_default');         $testEntity = $em-&gt;getRepository('v1\\Entity\\Test')-&gt;findAll();                  \/\/\u0434\u0430 \u043d\u0435 \u0445\u043e\u0440\u043e\u0448\u0435\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442         foreach ($testEntity as $entity) {            $array[] = $entity-&gt;getJsonData();                   }         return new JsonModel(array('response' =&gt; $array));     }  } <\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c cli \u0441\u0435\u0440\u0432\u0435\u0440 php -S localhost:8000 \u0438\u0437 \u043f\u0430\u043f\u043a\u0438 public, \u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435 <a href=\"http:\/\/localhost\">localhost<\/a>:8000\/api\/v1\/index, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043e\u0442\u0432\u0435\u0442 \u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/p>\n<p>  P.S.<br \/>  github: <a href=\"https:\/\/github.com\/CawaKharkov\/zf2-api\">zf2-api<\/a><br \/>  \u0415\u0441\u043b\u0438 \u0432\u043e\u0437\u043d\u0438\u043a\u043b\u0438 \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0432\u043e\u043f\u0440\u043e\u0441\u044b, \u043f\u0438\u0448\u0438\u0442\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438, \u0441 \u0440\u0430\u0434\u043e\u0441\u0442\u044c\u044e \u043e\u0442\u0432\u0435\u0447\u0443.<br \/>  \u0415\u0441\u043b\u0438 \u043a\u043e\u0433\u043e-\u0442\u043e \u0437\u0430\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u0435\u0442, \u043c\u043e\u0433\u0443 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u044b\u043c \u0433\u0430\u0439\u0434\u043e\u043c. \t\t\t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/199502\/\"> http:\/\/habrahabr.ru\/post\/199502\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">\n<h4>\u041d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0433\u0430\u0439\u0434 \u043f\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u0433\u043e RESTful api \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Zend framework 2 (\u0434\u0430\u043b\u0435\u0435 zf2).<\/h4>\n<p>  \u041d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f zf2 v2.3@dev, \u0430 \u0442\u0430\u043a \u0436\u0435 Doctrine 2 ORM.<\/p>\n<p>  \u0418\u0442\u0430\u043a, \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0439 \u043d\u0430\u0448\u0435\u0433\u043e api:<\/p>\n<pre><code class=\"bash\">mkdir -pv zf2-api\/{config\/autoload,public,module\/v1\/{config,src\/v1\/{Controller,Service,Entities}}}<\/code><\/pre>\n<p>  \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u043a\u043e\u043c\u043f\u043e\u0437\u0435\u0440   <\/p>\n<pre><code class=\"bash\">curl -sS https:\/\/getcomposer.org\/installer | php<\/code><\/pre>\n<p>  \u0422\u0430\u043a \u0436\u0435 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c composer.json \u0441 \u0442\u0430\u043a\u0438\u043c\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044f\u043c\u0438 \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0438\u0445.  <\/p>\n<pre><code class=\"css\">&quot;require&quot;: {         &quot;php&quot;: &quot;&gt;=5.4&quot;,         &quot;zendframework\/zendframework&quot;: &quot;2.*@dev&quot;,         &quot;doctrine\/doctrine-orm-module&quot; :&quot;0.*&quot;     } <\/code><\/pre>\n<p>  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-199502","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/199502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=199502"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/199502\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=199502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=199502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=199502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}