覆盖zfcuseradmin控制器

时间:2013-02-07 13:34:50

标签: controller override zend-framework2

我是zf2的新手,我试图覆盖zfcuseradmin / useradmincontroller 但它不起作用。我尝试了很多东西,但无法让它发挥作用 这也是我在这里的第一篇文章,请原谅我,如果它不以任何方式正确。

我在名为MyZfcUserAdmin的模块文件夹中创建了一个文件夹 我也在使用bjuauthorize,但不知道这对你们来说是否重要。 引导程序正在运行,因为formfileds已添加到我的zfcuseradmin创建表单

这是我的代码:

模块/ MyzfcUserAdmin /配置/ module.config.php

return array( 
'controllers' => array(
    'invokables' => array(
        'MyZfcUserAdmin' => 'MyZfcUserAdmin\Controller\MyZfcUserAdminController',
    ),
),

);

模块/ MyzfcUserAdmin / SRC / MyzfcUserAdmin /控制器/ MyZfcUserAdminController.php

<?php
namespace MyZfcUserAdmin\Controller;

use ZfcUserAdmin\Controller\UserAdminController as BaseUserAdminController;

class MyZfcUserAdminController extends BaseUserAdminController
{

public function createAction()
{
    $result = parent::createAction();
    $form = $this->getServiceLocator()->get('zfcuseradmin_createuser_form');        
    $request = $this->getRequest();

    $service = $this->getAdminUserService();

    $messages = array();
    if ($request->isPost()) 
    {

        $data = $request->getPost()->toArray();
        $data['governance'] = 1;


        if ($service->createasd($data) ) 
        {
            $messages[] = array(
                'type'    => 'success',
                'icon'    => 'icon-ok-sign',
                'message' => 'succes',
            );
            return $this->redirect()->toRoute('zfcadmin/zfcuseradmin/list');
        }
        else
        {
            $messages[] = array(
                'type'    => 'error',
                'icon'    => 'icon-remove-sign',
                'message' => 'failed',
            );

        }
    }

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

模块/ MyzfcUserAdmin / Module.php

<?php

namespace MyZfcUserAdmin;


use BjyAuthorize\Provider\Role\ZendDb AS rolesDb;

class Module
{
    public $rights = array();
    public $governance = array();

    public function onBootstrap($e)
    {

        //get roles
        $serviceManager = $e->getTarget()->getServiceManager();
        $rolesdb = new rolesDb('', $serviceManager);

        $roles = $rolesdb->getRoles();
        $roleOptions = array('');
        foreach($roles AS $role)
        {
            $name = $role->getRoleId();
            if($name != 'guest')
                $roleOptions[$name] = ucfirst($name);
        }
        $this->rights = array(
                'name' => 'role',
                'type' => 'Zend\Form\Element\Select',     
                'attributes' => array(
                        'required' => 'required',
                ),
                'options' => array(
                        'label' => 'Rights',
                        'value_options' => $roleOptions,
                )
            );   

        $this->governance = array(
                'name' => 'governance',
                'options' => array(
                        'label' => 'Governance',
                        'checkedValue'   => 1,
                        'uncheckedValue' => 0,
                ),
                'attributes' => array(
                        'type' => 'checkbox',
                ),
            );   


        $app = $e->getParam('application');
        $em  = $app->getEventManager()->getSharedManager(); 
        $em->attach('ZfcUserAdmin\Form\CreateUser', 'init', function($e) {
            $form = $e->getTarget();
            //$form->add($this->rights);
            $form->add($this->governance);        

        });
        $em->attach('ZfcUserAdmin\Form\EditUser', 'init', function($e) {
            $form = $e->getTarget();
           // $form->add($this->rights);
            $form->add($this->governance);
        });
        $em->attach('ZfcUserAdmin\Form\EditUserFilter','init', function($e) {
            $filter = $e->getTarget();
            // Do what you please with the filter instance ($filter)

        });

    }




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

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


}

1 个答案:

答案 0 :(得分:1)

您可以在module.config.php中覆盖路由

使用路径定义中的默认控制器替换控制器。

....
'zfcuser' => array(
            'type' => 'Literal',
            'priority' => 1000,
            'options' => array(
                'route' => '/user',
                'defaults' => array(
                    'controller' => 'myzfcuser',
                    //'controller' => 'zfcuser',
                    'action'     => 'index',
                ),
            ),

同时查看zfcuser.global.php配置,你可以在那里修改路由选项

/**
 * Login Redirect Route
 *
 * Upon successful login the user will be redirected to the entered route
 *
 * Default value: 'zfcuser'
 * Accepted values: A valid route name within your application
 *
 */
//'login_redirect_route' => 'zfcuser',

/**
 * Logout Redirect Route
 *
 * Upon logging out the user will be redirected to the enterd route
 *
 * Default value: 'zfcuser/login'
 * Accepted values: A valid route name within your application
 */
//'logout_redirect_route' => 'zfcuser/login',