如果我有两个标签,如何使用不同的控制器在不同的视图中显示数据,如何配置module.php

时间:2019-05-08 14:11:36

标签: php zend-framework

如何配置module.php? 我试图使用不同的控制器从具有2个不同视图的两个表中获取数据。 下面是我的代码,向您显示我在模块,业务控制器和注册控制器中正在做什么。

Module.php

public function getServiceConfig(){
    return array(
        'factories' => array(
            // Instantiating StudentTable class by injecting TableGateway
            'TaskForce\Model\RegistrationTable'=>function($sm){
                $registerationGateway = $sm->get('RegistrationTableGateway');
                $table = new RegistrationTable($registerationGateway);
                return $table;
            },
            //Instantiating TableGateway to inject to StudentTable class
            'RegistrationTableGateway'=>function($sm){
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new Registration());
                return new TableGateway('users', $dbAdapter,null,$resultSetPrototype);
            },
            'TaskForce\Model\BusinessTable'=>function($sm){
                $businessGateway = $sm->get('BusinessTableGateway');
                $businesstable = new BusinessTable($businessGateway);
                return $businesstable;
            },
            //Instantiating TableGateway to inject to StudentTable class
            'BusinessTableGateway'=>function($sm){
                $dbAdapter2 = $sm->get('Zend\Db\Adapter\Adapter');
                $resultSetPrototype2 = new ResultSet();
                $resultSetPrototype2->setArrayObjectPrototype(new Business());
                return new TableGateway('business', $dbAdapter2,null,$resultSetPrototype2);
            },
        )
    );
}

BusinessController.php

public function indexAction(){
    // Setting layout for this action
    $this->layout("layout/main_layout");
    return new ViewModel(array(
        // Fetching data from database
        'business'=>$this->getBusinessTable()->fetchAll()
    ));
}

和RegistrationController.php

public function indexAction(){
    // Setting layout for this action
    $this->layout("layout/main_layout");

    $form = new RegistrationForm();

    $request = $this->getRequest();

    if ($request->isPost()) {

        $registration = new Registration();

        //$form->get('submit')->setAttribute('value', 'Add New User');

        $post = array_merge_recursive(
            $request->getPost()->toArray(),
            $request->getFiles()->toArray()
        );
        $form->setData($post);

        if ($form->isValid()) {

            $registration->exchangeArray($form->getData());

            $this->getRegistrationTable()->saveRegistration($registration);

            // Redirect to list of users

            return $this->redirect()->toRoute('registration');
        }
    }

    return new ViewModel(array(
        'form' => $form
    ));
}

1 个答案:

答案 0 :(得分:0)

如果企业与注册记录之间存在关联,则可能需要澄清。但是,如果仅仅是代码安排的问题,则可以执行以下操作:

BusinessController.php

public function indexAction(){
    // Setting layout for this action
    $this->layout("layout/main_layout");
    return new ViewModel(array(
        // Fetching data from database
        'business'=>$this->getBusinessTable()->fetchAll(),
        'registration' => $this->getRegistrationTable()->fetchAll(),
    ));
}

RegistrationController.php

public function indexAction(){
    // Setting layout for this action
    $this->layout("layout/main_layout");
    return new ViewModel(array(
        // Fetching data from database
        'business'=>$this->getBusinessTable()->fetchAll(),
        'registration' => $this->getRegistrationTable()->fetchAll(),
    ));
}

// rename your index action in registrationcontroller.php to form 
// so that the form is displayed on the formAction
public function formAction(){
    // Setting layout for this action
    $this->layout("layout/main_layout");

    $form = new RegistrationForm();

    $request = $this->getRequest();

    if ($request->isPost()) {
        $registration = new Registration();
        //$form->get('submit')->setAttribute('value', 'Add New User');

        $post = array_merge_recursive(
            $request->getPost()->toArray(),
            $request->getFiles()->toArray()
        );
        $form->setData($post);

        if ($form->isValid()) {
            $registration->exchangeArray($form->getData());
            $this->getRegistrationTable()->saveRegistration($registration);

            // Redirect to list of users
            return $this->redirect()->toRoute('registration');
        }
    }

    return new ViewModel(array(
        'form' => $form
    ));
}