Welcome to Abdul Malik Ikhsan's Blog

Conditional Redirect on ZfcUser when login success

Posted in Tutorial PHP, Zend Framework 2 by samsonasik on March 15, 2015

I have been digging with ZfcUser in this week, and I realize, I need to do conditional redirection when authentication successful. With current version ( 1.2.2 ), the ‘login_redirect_route’ only support ‘string’ value,  which is very strict. We currently can’t pass callable to it like the following code ( hopefully, in next version or  2.* we can do ) :

'login_redirect_route' => function(\ZfcUser\Entity\UserInterface $user) {
    if ($user->getRole()->getName() === 'admin') {
        return 'zfcadmin';
    return 'zfcuser';

We need another approach right now!


1. ZfcUser\Options\ModuleOptions

The ZfcUser configuration is consumed by ZfcUser\Options\ModuleOptions, registered as ‘zfcuser_module_options’ service, so, we can use it to re-set the ‘loginRedirectRoute’ property value, and re-register as service.

2. ‘authenticate.success’ event

‘authenticate.success’ is an event that will be triggered when we catch that user has Identity on Authentication process, in ZfcUser\Authentication\Adapter\AdapterChain::prepareForAuthentication.


With 2 clues above, What we can do now is, using listener that re-set ‘zfcuser_module_options’ service which bring new ‘loginRedirectRoute’, that react to ‘authenticate.success’. We can do in our Module class like the following :

use Application\Entity\User;
class Module
    // ...
    public function onBootstrap($e)
        $servicemaManager = $e->getTarget()->getServiceManager();
        $objectManager    = $serviceManager->get('objectManager');

        $zfcAuthEvents    =  $serviceManager->get('ZfcUser\Authentication\Adapter\AdapterChain')->getEventManager();
        $zfcAuthEvents->attach('authenticate.success', function($authEvent) use ($serviceManager, $objectManager) {
            $userId = $authEvent->getIdentity();
            $user   = $objectManager->find(User::class, $userId);

            if ($user->getRole()->getName() === 'admin') {
                // we are going to re-set service,
                // we need to set allow_override of serviceManager= true

                $zfcuserModuleOptions = $serviceManager->get('zfcuser_module_options');
                $serviceManager->setService('zfcuser_module_options', $zfcuserModuleOptions);

                // set 'allow_override' back to false

Now, whenever we got role name = ‘admin’ during authentication process, we will be redirected to ‘zfcadmin’ route.

References :
1. http://stackoverflow.com/questions/16053479/zfcuser-redirect-roles-to-different-pages-after-login
2. Conversation with Mr. Daniel Strøm
3. http://circlical.com/blog/2013/7/5/capturing-auth-events-with-zfcuser

8 Responses

Subscribe to comments with RSS.

  1. Julián Gorge said, on March 15, 2015 at 3:34 am

    Great!! can you make an conditional redirect to doctrine 2 auth?

    • samsonasik said, on March 15, 2015 at 3:37 am

      I did above with “zf-commons/zfc-user-doctrine-orm” which is using doctrine😛

      • Julián Gorge said, on March 19, 2015 at 11:56 pm

        excuse me, hi! and if i dont use zfc-user? only doctrine authentication?

      • samsonasik said, on March 20, 2015 at 1:21 am

        that’s even easier, just modify in your controller😛

  2. nomaanp153 said, on May 6, 2015 at 7:25 pm

    Hello Samsonasik,
    Can you please give an example of setting the role of user using zend db?

  3. Ruben van Engelenburg said, on May 11, 2015 at 11:36 pm

    Great article, thanks! Any ideas to redirect to a URL instead of a predefined route?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: