1

这是我的第一堂课

<?php 

namespace Config\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Config\Model\Config;
use Config\Form\ConfigForm;

class ConfigController extends AbstractActionController
{ 
protected $configTable;



public function indexAction()
{
    $this->getSMTPConfigTable();
    return new ViewModel(array(
            'config' => $this->getConfigTable()->fetchAll(),
    ));

}

public function addAction()
{
    $form = new ConfigForm();
    $form->get('submit')->setValue('Add');

    $request = $this->getRequest();
    if ($request->isPost()) {
        $config = new Config();
        $form->setInputFilter($config->getInputFilter());
        $form->setData($request->getPost());

        if ($form->isValid()) {
            $config->exchangeArray($form->getData());
            $this->getConfigTable()->saveConfig($config);

            return $this->redirect()->toRoute('zfcadmin/config');
        }
    }
    return array('form' => $form);
}

public function editAction()
{
    $id = (int) $this->params()->fromRoute('id', 0);
    if (!$id) {
        return $this->redirect()->toRoute('zfcadmin/config', array(
                'action' => 'add'
        ));
    }

    try {
        $config = $this->getConfigTable()->getConfig($id);
    }
    catch (\Exception $ex) {
        return $this->redirect()->toRoute('zfcadmin/config', array(
                'action' => 'index'
        ));
    }

    $form  = new ConfigForm();
    $form->bind($config);
    $form->get('submit')->setAttribute('value', 'Edit');

    $request = $this->getRequest();
    if ($request->isPost()) {
        $form->setInputFilter($config->getInputFilter());
        $form->setData($request->getPost());

        if ($form->isValid()) {
            $this->getConfigTable()->saveConfig($form->getData());

            return $this->redirect()->toRoute('zfcadmin/config');
        }
    }

    return array(
            'id' => $id,
            'form' => $form,
    );
}

public function getConfigTable()
{
    if (!$this->configTable) {
        $sm = $this->getServiceLocator();
        $this->configTable = $sm->get('Config\Model\ConfigTable');
    }
    return $this->configTable;
}

public function getSMTPConfigTable()
{
    $pr=$this->getConfigTable()->fetchAll();
    return $pr;
}

    }

在另一个模块类中我该怎么做?我尝试了大多数方法,但我失败了,请帮助我。(对不起我的英语)我需要 $temp=new ConfigController(); $temp->getSMTPConfigTable();

4

3 回答 3

4

我首先想检查我是否理解您的问题正确。阅读问题的名称和内容,我假设您询问了一种在另一个控制器或模块类中获取 ConfigTable(模型)的方法。

要了解如何在 ZF2 中管理对象和服务,我建议您查看此 ZF2 手册页:

http://framework.zend.com/manual/2.0/en/modules/zend.service-manager.quick-start.html

如果您注意该页面的“创建 ServiceLocator-aware 类”部分,您会发现您可以通过服务管理器本身访问向服务管理器注册的任何对象。服务管理器在其术语中被注入到每个实现ServiceLocatorAwareInterface或简单地具有名为的方法的对象中setServiceLocator()

清楚了这一点,ZF2Zend\Mvc\Controller\AbstractActionController实现了这个接口,这意味着您可以在对象中获取服务管理器,因此可以在其中注册任何其他对象。如果您查看以下方法(来自您的Config\Controller),则有一个从服务管理器获取模型的好示例:

public function getConfigTable()
{
    if (!$this->configTable) {
        $sm = $this->getServiceLocator();
        $this->configTable = $sm->get('Config\Model\ConfigTable'); // <-- HERE!
    }
    return $this->configTable;
}

ServiceManager因此,在通过并且具有setServiceLocator()(和 optionaly )方法创建的每个类中,getServiceLocator()您可以以相同的方式访问您的模型:

$sm = $this->getServiceLocator();
// or 
$sm = $this->serviceLocator; // or whatever attribute you have set it to.
$configTable = $sm->get('Config\Model\ConfigTable');

关于如何调用fetchAll()你的模型的方法,我把这个决定留给你在哪里做。

于 2013-03-27T14:30:57.260 回答
2

这正是您使用ServiceManager

您可以将您的TableGateway(或您用作“模型表”的任何东西)注册到ServiceManager如下所示:

//Module.php 
public function getServiceConfig()
{
    return array(
        'factories' => array(
            'my-model-gateway' => function($sm) {
               $gw = new MyTableGateway($sm->get('Zend\Db\Adapter\Adapter'));
               return $gw;
            }
        )
    }
}

这只是一个展示场景——它不会像这样对你进行复制粘贴——我没有从你的代码中变得聪明,但你可以使用这个简单的例子来知道做了什么。我注册了一个名为的服务my-model-gateway,该服务创建了一个名为MyTableGateway. 此类将Zend\Db\Adapter\Adapter通过构造函数注入默认注入。

使用它,您可以从任何模块的任何控制器访问此服务,如下所示:

$gw = $this->getServiceLocator()->get('my-model-gateway');

希望这可以让你开始。

于 2013-03-27T14:30:15.263 回答
0

为此,您将需要一个构造函数。试试这个:

class ConfigController {
   function __construct() {
   }
}

即使它是空的,您也将获得该类的实例,然后您将能够访问所有参数。也许您还需要为类的一般设置创建一个 init。

于 2013-03-27T13:25:05.540 回答