I’m new to Zend Framework 2. I’m starting a project and I want its security being managed by ZfcAdmin, ZfcUser, ZfcUserAdmin and BjyAuthorize. The first thing I’m trying to do is modify the creation users’ process. I want to be able to assign roles to a new user right after its creation.
The first problem I’m facing is when a user is created, the controller redirects me to the users’ list page. I need to change this behaviour, I want to be redirected to the edit page, where I’ll be able to choose N roles for the recently created user (that’ll another war with entities…). I’ve chosen to override the UserAdminController (ZfcUserAdmin). That’s what I’ve done to achieve that:
1. I load my administration module (MyAdministration) in the last place in application.config.php,
in order to be able to override other modules’ properties.
2. I override ZfcUserAdmin controller in MyAdministration/config/module.config.php in order to use mine:
(...)
'controllers' => array(
'invokables' => array(
'zfcuseradmin' => 'MyAdministration\Controller\MyAdministrationController',
),
),
(...)
3. I’ve created the class
MyAdministration/src/MyAdministration/Controller/MyAdministrationController.php
4. I’ve declared it to extend the ZfcUserAdmin one
namespace Administracion\Controller;
(...)
use ZfcUserAdmin\Controller\UserAdminController;
class AdministracionController extends UserAdminController {
(...)
5. I’ve overridden the createAction function to redirect to the edit page
(...)
public function createAction() {
(...)
return $this->redirect()->toRoute('zfcadmin/zfcuseradmin/edit/:userId',
array('userId' => $user->getId()));
}
(...)
That’s where I don’t know if I made it right.
Searching the net and debugging I’ve learned that there’s a class called InjectTemplateListener which transform the Controller’s namespace into a path to the desired template. My controller ‘is translated’ to
my-administration/my-administration/edit which leads to nowhere, the templates belong to ZfcUserAdmin module. The right path is the one obtained by its controller (ZfcUserAdmin\Controller\UserAdminController):
zfc-user-admin/user-admin/edit
I also learned that template paths can be written manually. Those paths are ignored by
InjectTemplateListener. That’s the approach I’ve used. In
MyAdministration/config/module.config.php I’ve written:
(...)
'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’m not sure if that’s the best way to achieve that. I feel there must be a better way to do that, instead of manually write template paths. I’ve found few things about overriding
Controllers, and no examples… Is this ok? Does anyone have a better approach to do the overriding?
Thanks!
You are correct the template name can be injected by a listener. That is under this single one condition: if there is no template name set.
So for this action the inject template listener injects the template name:
The template will be
my-module/my-controller/my-action. However, if you set the template, the listener will be skipped:You can see in the controller you are overriding the returned data with the form is a simple array and not even a view model. The redirect plugin returns a Response object.
So, you check whether the return value is an array and if so, you set the template explicitly. This skips the listener to inject the template name:
Because you set the template name directly now, you can skip your manipulation of the template map in your config. This also enhances flexibility because you hard-coded the template paths with paths outside your module. You also have the option now to override the zfcUserAdmin template map in another module.