Zfcuser route has a child route

58 Views Asked by At

I'm working on a ZF2 project, and using ZfcUser to manage website users. I just want to know is it possible to have a child route to the zfcuser route? Something like that, in the configuration of my module:

return [
        'router' =>
        [
            'routes' =>
            [
                'admin' =>
                [
                    'type'    => 'Segment',
                    'options' =>
                    [
                        'route'    => '/admin',
                        'defaults' =>
                        [
                            '__NAMESPACE__' => 'admin',
                            'controller'    => 'admin.index',
                            'action'        => 'index',
                        ],
                    ],
                    'may_terminate' => true,
                    'child_routes'  =>
                    [
                        'zfcuser' =>
                        [
                            'type' => 'Literal',
                            'options' =>
                            [
                                'route' => '/account',
                            ]
                        ],
                    ],
                ],

            ],
        ],
    ];
1

There are 1 best solutions below

2
adamdyson On

This is a problem I've tried overcoming recently, not using ZfcUser but whilst developing my own modules. There are a few solutions but the most suitable would depend on the type of application you're developing.

The easiest solution would be to override the zfcuser route path and prefixing it with admin.

return [
    'router' => [
        'routes' => [
            'zfcuser' => [
                'options' => [
                    'route' => '/admin/user',
                ],
            ],
        ],
    ],
];

If you're like me and want all you admin routes contained under a single route then you're better off removing the zfcuser route completely and implementing your own which could utilize the ZfcUser controllers.

namespace Application;

use Zend\ModuleManager\ModuleEvent;
use Zend\ModuleManager\ModuleManager;

class Module
{
    public function init(ModuleManager $moduleManager)
    {
        $events = $moduleManager->getEventManager();
        $events->attach(ModuleEvent::EVENT_MERGE_CONFIG, array($this, 'onMergeConfig'));
    }

    public function onMergeConfig(ModuleEvent $event)
    {
        $configListener = $event->getConfigListener();
        $configuration = $configListener->getMergedConfig(false);

        if (isset($configuration['router']['routes']['zfcuser']))
        {
            unset($configuration['router']['routes']['zfcuser']);
        }

        $configListener->setMergedConfig($configuration);
    }
}