我有一個類別控制器與dbase的fetchSingle(返回1記錄)和fetchAll(返回所有)方法。Zend2導航全局佈局
現在我想重新使用de fetchall方法導航。
什麼是最好的方法?
以下代碼:
Module.php
public function getServiceConfig()
{
return array(
'factories' => array(
/*
* Category Table
*/
'CategoryTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
return new TableGateway('category', $dbAdapter, null, $resultSetPrototype);
},
'CategoryTable' => function($sm) {
$tableGateway = $sm->get('CategoryTableGateway');
$table = new Model\CategoryTable($tableGateway);
return $table;
},
),
);
}
CategoryController:
namespace Front\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class CategoryController extends AbstractActionController
{
protected $categoryTable;
function indexAction() {
//$slug = $this->getEvent()->getRouteMatch()->getParam('slug');
$this->layout()->setVariable('metatitle', 'title1');
$this->layout()->setVariable('metadescription', 'description2');
$this->layout()->setVariable('metakeywords', 'keywords3');
return new ViewModel(array(
'category' => $this->getCategories(),
));
}
public function getCategory($slug){
return $this->getTable()->getCategory($slug);
}
public function getCategories(){
return $this->getTable()->getCategories();
}
public function getTable()
{
if (!$this->categoryTable) {
$sm = $this->getServiceLocator();
$this->categoryTable = $sm->get('CategoryTable');
}
return $this->categoryTable;
}
}
CategoryModel:
namespace Front\Model;
use Zend\Db\TableGateway\TableGateway;
class CategoryTable {
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function getCategory($slug)
{
$resultSet = $this->tableGateway->select(array('name' => $slug));
$resultSet = $resultSet->toArray();
return $resultSet[0];
}
public function getCategories()
{
$resultSet = $this->tableGateway->select();
$resultSet = $resultSet->toArray();
return $resultSet;
}
}
你想要例如在每個頁面上列出的所有類別,或使用Zend \ Navigation列出某些指定頁面上的所有這些類別? –
@JurianSluiman嗨Jurian,是的,我有一個dbtable類別,我想做一個導航。我看到你可以在配置中根據導航數組創建一個,但我想從數據庫中取出它。我已經有一個類的類工作,現在我想讓導入控制器的een實例,並顯示在layout.phtml – Bas
@JurianSluiman,thnx爲您的答覆!看起來不錯,只有一件我不明白的是存儲。這與我的類別模型相同,我已經擁有或者是不同的存儲空間。你能舉一個例子或解釋嗎? – Bas