Zend框架2,分层路由,JSon Return

时间:2015-12-29 21:00:33

标签: php json rest zend-framework zend-framework2

我是zend框架的新手。我已经想过教程并有一个工作网址,但它不能像我需要的那样工作。 我正在使用Zend Framework 2.4。

http://localhost/part/3 它以json格式返回id为3的所有部分(这就是我想要的)

http://localhost/part/3/patents 仍然只返回json格式为id为3的所有部分。我想要的是返回第3部分的所有专利

首先走的是不同的默认操作会做到这一点的路径,但它似乎不适用于ViewJsonStrategy'你无法根据我的知识设置默认操作。也试过这个帖子的onBootstrap()版本,但不能让它工作。 Zend_Rest_Route and hierarchical routes 我试过的最后一件事是在黑暗中拍摄的另一个控制器专利没有工作。

所以我的问题是这些正确的路径,如果是的话,我做错了什么,如果不是,那我该怎么办?  这是我的module.config.php

 return array(
     'controllers' => array(
         'invokables' => array(
        'Part\Controller\Part' => 'Part\Controller\PartController',
        'Part\Controller\Patent' =>'Part\Controller\PatentController'
    ),
),

// The following section is new` and should be added to your file
'router' => array(
    'routes' => array(
        'part' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'    => '/v1.0/part[/:id]', 
                'constraints' => array(
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Part\Controller\Part',
                   // 'action' => 'index'
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'patent' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/patent',
                        'defaults' => array(
                            'controller' => 'Part\Controller\part',
                        )

                    ),
                ),
            ),
        ),
    ),
),
   'view_manager' => array(
   'strategies' => array(
       'part' => 'ViewJsonStrategy',
      'patent' => 'ViewJsonStrategy',
    ),
),
);

这是我的Module.php

namespace Part;

use Part\Model\PartTable;
use Part\Model\Patent;

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

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'Part\Model\PartTable' =>  function($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $table = new PartTable($dbAdapter);
                return $table;
            },
             'Part\Model\Patent' =>  function($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $table = new \Patent($dbAdapter);
                return $table;
            },
        ),
    );
}    

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

这是我的PartController.php

namespace Part\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;

class PartController extends AbstractRestfulController
{
protected $partTable;

public function getList()
{
    $results = $this->getPartTable()->fetchAll();
    $data = array();
    foreach($results as $result) {
        $data[] = $result;
    }

    return new JsonModel(array(
        'data' => $data,
    ));
}
public function get($id)
{
   $results = $this->getPartTable()->fetchById($id);
   $data = array();
    foreach($results as $result) {
        $data[] = $result;
    }

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

public function create($data)
{

}

public function update($id, $data)
{

}

public function delete($id)
{

}
public function getPartTable()
{
    if (!$this->partTable) {
        $sm = $this->getServiceLocator();
        $this->partTable= $sm->get('Part\Model\PartTable');
    }
    return $this->partTable;
}
}

这是我的PatentController.php

namespace Part\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;

class PatentController extends AbstractRestfulController
{
protected $patent;

public function getList()
{
    $results = $this->getPatent()->fetchAllBooks();
    $data = array();
    foreach($results as $result) {
        $data[] = $result;
    }

    return new JsonModel(array(
        'data' => $data,
    ));
}
public function get($id)
{
   $results = $this->getPatent()->fetchById($id);
   $data = array();
    foreach($results as $result) {
        $data[] = $result;
    }

    return new JsonModel(array(
        'data' => $data,
    ));
}
public function getPatent()
{
    if (!$this->patent) {
        $sm = $this->getServiceLocator();
        $this->patent= $sm->get('Part\Model\part');
    }
    return $this->partTable;
}
}

这是我的PartTable.php

  namespace Part\Model;

use Zend\Db\Adapter\Adapter;
class PartTable 
{


public function __construct(Adapter $adapter)
{
    $this->adapter = $adapter;
}

public function fetchAll()
{
    $statement = $this->adapter->createStatement('SELECT * FROM test.album');
    $result = $statement->execute();
    return $result;

}
public function fetchById($id){
    $statement = $this->adapter->createStatement('SELECT * FROM test.album where id = '.$id);
    $result = $statement->execute();
    return $result;
}

}

这是我的Patent.php

namespace Patent\Model;

use Zend\Db\Adapter\Adapter;
class Patent
{

public function __construct(Adapter $adapter)
{
    $this->adapter = $adapter;
}

public function fetchAll()
{
    $statement = $this->adapter->createStatement('SELECT * FROM test.book');
    $result = $statement->execute();
    return $result;

}
 public function fetchById($id){
    $statement = $this->adapter->createStatement('SELECT * FROM test.book where id = '.$id);
    $result = $statement->execute();
    return $result;
}
}

1 个答案:

答案 0 :(得分:1)

配置中有拼写错误。而不是:

'child_routes' => array(
                'patent' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/patent',
                        'defaults' => array(
                            'controller' => 'Part\Controller\part',
                        )

                    ),
                ),
            ),

你应该:

'child_routes' => array(
                'patent' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/patents',
                        'defaults' => array(
                            'controller' => 'Part\Controller\Patent',
                        )

                    ),
                ),
            ),