Switching the controller in ZF2

I am new to Zend Framework 2. I am running a project and I want its security to be managed by ZfcAdmin, ZfcUser, ZfcUserAdmin and BjyAuthorize. The first thing I'm trying to do is change the user creation process. I want to be able to assign roles to a new user immediately after creating it.

The first problem I encounter is when the user is created, the controller redirects me to the user list page. I need to change this behavior, I want to be redirected to the editing page, where I can select N roles for a newly created user (this will be another war with entities ...). I decided to override UserAdminController (ZfcUserAdmin). This is what I did for this:

1. I load my administrative module (MyAdministration) in last place in application.config.php to be able to override the properties of other modules.

2. I redefine the ZfcUserAdmin controller in MyAdministration / config / module.config.php to use mine:

(...) 'controllers' => array( 'invokables' => array( 'zfcuseradmin' => 'MyAdministration\Controller\MyAdministrationController', ), ), (...) 

3. I created the class MyAdministration / SRC / MyAdministration / Controller / MyAdministrationController.php

4. I announced that it extends ZfcUserAdmin one

 namespace Administracion\Controller; (...) use ZfcUserAdmin\Controller\UserAdminController; class AdministracionController extends UserAdminController { (...) 

5. I redefined the createAction function to redirect to the edit page

 (...) public function createAction() { (...) return $this->redirect()->toRoute('zfcadmin/zfcuseradmin/edit/:userId', array('userId' => $user->getId())); } (...) 

What if I donโ€™t know if I did it right. Network Search and Debugging I found out that there is a class called InjectTemplateListener that converts the Controller namespace to the path to the desired template. My controller is "transferred" to my administration / my administration / editing , which leads to nothing, the templates belong to the ZfcUserAdmin module. The correct path is the one received by its controller (ZfcUserAdmin \ Controller \ UserAdminController): ZFC-user-administrator / user-administrator / change

I also found out that template paths can be written manually. These paths are ignored by InjectTemplateListener. This is the approach I used. In MyAdministration / config / module.config.php I wrote:

 (...) 'view_manager' => array( 'template_map' => array( 'my-administration/my-administration/list' => __DIR__ . '/../../../vendor/ZfcUserAdmin/view/zfc-user-admin/user-admin/list.phtml', 'my-administration/my-administration/create' => __DIR__ . '/../../../vendor/ZfcUserAdmin/view/zfc-user-admin/user-admin/create.phtml', 'my-administration/my-administration/edit' => __DIR__ . '/../../../vendor/ZfcUserAdmin/view/zfc-user-admin/user-admin/edit.phtml', 'my-administration/my-administration/pagination_userlist' => __DIR__ . '/../../../vendor/ZfcUserAdmin/view/zfc-user-admin/user-admin/pagination_userlist.phtml', ), (...) ), 

I am not sure if this is the best way to achieve this. I believe that there should be a better way to do this, instead of manually writing template templates. I found a few things about overriding Controllers, and there are no examples ... Is this normal? Does anyone have a better approach to do redefinition?

Thanks!

+4
source share
1 answer

You are correct, the name of the template can be entered by the listener. This is under one single condition: if there is no template name.

So, for this action, the injection template receiver enters the name of the template:

 namespace MyModule; class MyController { public function myAction() { return new ViewModel; } } 

The template will be my-module/my-controller/my-action . However, if you install the template, the listener will be skipped:

 namespace MyModule; class MyController { public function myAction() { $view = new ViewModel; $view->setTemplate('another-module/my-controller/my-action'); return $view; } } 

You can see that in the controller you override the returned data with the form - this is a simple array and even a view model . The redirect plugin returns a Response object.

So, you are checking to see if the return value is an array, and if so, you are setting the template explicitly . This skips the listener to enter the template name:

 namespace MyAdminModule; use ZfcUserAdmin\Controller\UserAdminController as BaseUserAdminController; use Zend\View\Model\ViewModel; use Zend\Http\Response; class UserAdminController extends BaseUserAdminController { public function createAction() { $result = parent::createAction(); if ($result instanceof Response) { // Old behaviour return $this->redirect()->toRoute('zfcadmin/zfcuseradmin/edit/:userId', array('userId' => $user->getId())); } // $result is array $view = new ViewModel; $view->setVariables($result); $view->setTemplate('zfc-user-admin/user-admin/create'); return $view; } } 

Since you are setting the template name right now, you can skip manipulating the template map in your configuration. It also increases flexibility as you hard-coded template paths with paths outside of your module. Now you also have the option to override the zfcUserAdmin template map in another module.

+3
source

All Articles