Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9070345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:40:04+00:00 2026-06-16T17:40:04+00:00

I’m new to Zend Framework 2. I’m starting a project and I want its

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:40:05+00:00Added an answer on June 16, 2026 at 5:40 pm

    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:

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

    The template will be my-module/my-controller/my-action. However, if you set 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 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:

    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;
        }
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm interested in microtypography issues on the web. I want a tool to fix:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to show the soap response to UIWebview.. my soap response is, <p><img
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.