2012-10-30 32 views
-1

我需要在我的zend框架项目中向其他控制器发送内部请求。使用Zend Framework执行内部请求1.11

我已经调查了操作助手这件事,但似乎没有工作。

我的项目是一个API。该API有时会重复输出。

举例: /client.json:返回客户端的列表,用户可以访问 /client/tree.json返回客户

为了减少模型代码和额外的查询重新绑定数据的树/客户端/tree.json最好是通过内部调用/client.json来获取已清理的客户端列表。

Zends文件说,这样的事情:

$request = clone $this->getRequest(); 
    $request->setActionName('get') 
     ->setControllerName('tree') 
     ->setParams(array('bar' => 'baz')); 
$this->_helper->actionStack($request); 

但是它不状态如何从请求中提取数据。如果我

print_r($this->_helper->actionStack($request)); 

我只是得到一吨的Zend垃圾

+0

你可能会发布一些代码,可能有助于解释你在问什么?我在理解什么是控制器的内部请求时可能会遇到问题,因为控制器本身不应该做任何事情。 – RockyFord

+0

更新,谢谢回复。 – azz0r

回答

-1

。这是不是应该在一个控制器来完成。它应该在模型中处理。该模型提供数据,在这种情况下是客户端列表或客户端树。只有模型应该提供这些数据。你想要完成的实际上是一种缓存形式。您可以在模型或应用程序的内部和外部以多种不同方式缓存该数据。

您可能想要从探索如何在模型中实现identity map开始。

class someBaseMapper 
//an identity map can be as simple as a protected class variable with accessors 
protected $map = array(); 

/** 
    * Set value and name of entity in identity map. 
    * 
    * @param string $id 
    * @param object $entity 
    */ 
protected function setMap($id, $entity) 
    { 
     $this->map[$id] = $entity; 
    } 

    /** 
    * Get value of entity id from identity map. 
    * 
    * @param string $id 
    * @return string 
    */ 
    protected function getMap($id) 
    { 
     if (array_key_exists($id, $this->map)) { 
      return $this->map[$id]; 
     } 
    } 

然后使用您的地图:

//later in the same mapper 
public function findById($id) 
{ 
    //check map requested id 
    if ($this->getMap($id)) { 
     return $this->getMap($id); 
    } 
    //if no map match 
    $select = $this->getGateway()->select(); 
    $select->where('id = ?', $id); 

    $row = $this->getGateway()->fetchRow($select); 
    //create entity 
    $entity = $this->createEntity($row); 
    //add new entity to map 
    $this->setMap($row->id, $entity); 

    return $entity; 
} 

也可以对数据库或页面缓存退房Zend_cache。 还有几种可用于PHP的外部缓存工具,您可能会感兴趣。

+0

是的,在某种程度上,我使用Propel,它为我处理了很多。但是我不完全同意,因为它不能回答我的问题。我仍然想要在我的API中调用内部调用,并且需要一个真正的解决方案。 – azz0r

+0

显然我误解了你的问题。抱歉。 – RockyFord

相关问题