1
votes

I am trying to solve the routing problem I am facing right now with no luck (googled for hours, saw dozens of examples and solved questions but none works for me - the closest one is this Routing Zend Framework 2 Language in url but even this is not working for me).

I have created an SSO application (I mean the authentication part) and now I am porting it to ZF2 app (I have it almost working as I workarounded the router but need now the final routing to be done) where only these types of URLs are possible:

http[s]://domain.com/login/asfgsdgdsfgzsdgdsf/
http[s]://domain.com/login/tdhjsdgbndfnfgdnhd/
http[s]://domain.com/logout/asfgsdgdsfgzsdgdsf/
http[s]://domain.com/info/dthnzdfbdfhgnfsd/

Those login, logout or info parts are not the controllers nor actions but the methods names I then call on SSO service from within my IndexController and indexAction(). The rest part of the URL is the client application hash.

Now here is my router config which only matches the home route and when the other parts (method name [and hash]) are provided, I got 404 from the ZF2 app (so at least I am in the app context).

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type'      => 'Zend\Mvc\Router\Http\Segment',
                'options'   => array(
                    'route'     => '/',
                    'defaults'  => array(
                        'controller'    => 'SSO\Controller\Index',
                        'action'        => 'index',
                        'act'           => 'login', // by doing this I am able to workaround the router and work with SSO
                        'client'        => '<my_secret_hash>', // by doing this I am able to workaround the router and work with SSO
                    ),
                ),
                'child_routes' => array(
                    'action' => array(
                        'type'      => 'Zend\Mvc\Router\Http\Segment',
                        'options'   => array(
                            'route'     => '[/:act]',
                            'defaults'  => array(
                                'act' => 'login',
                            ),
                            'constraints' => array(
                                'act' => '(login|logout|info)?', // only login, logout or info are allowed
                            ),
                        ),
                        'child_routes' => array(
                            'client' => array(
                                'type'      => 'Zend\Mvc\Router\Http\Segment',
                                'options'   => array(
                                    'route'     => '[/:client]',
                                    'constraints' => array(
                                        'client'    => '[a-zA-Z0-9]+',
                                    ),
                                    'defaults'  => array(
                                        'client' => '<my_secret_hash>',
                                ),
                                'may_terminate' => true,
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
);

(only router part of my module.config is provided...)

Now with that router only http[s]://domain.com[/] is matched and either of http[s]://domain.com/(login|logout|info)[/] or http[s]://domain.com/(login|logout|info)/adfbsDVdfbzdbxfbndzxbxfnb[/] matches into A 404 error occured.

Though I try to define the route parts as optional (just for the testing and development purposes), they should by required in the production environment. Anyway, I tried to define them NOT optional too, but didn't work either.

Question: How should I configure the router to match my routes (URLs) defined in the beginning?

1

1 Answers

3
votes

Couple of things:

  • My guess is that you have too many slashes in there -- a child route of / doesn't need to be defined with a slash at the beginning since it's parent already got it
  • I don't see a reason for 2 nested routes (of course I don't know the rest of your config, so it's a guess)
  • home route probably should be Literal and able to terminate (again a guess)
  • there was one unmatched parenthesis (probably 'misspasted' or something)

I suppose this should work for you:

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Literal',
            'may_terminate' => true,
            'options'   => array(
                'route'     => '/',
                'defaults'  => array(
                    'controller'    => 'SSO\Controller\Index',
                    'action'        => 'index',
                ),
            ),
            'child_routes' => array(
                'whateva' => array(
                    'type'      => 'Segment',
                    'may_terminate' => true,
                    'options'   => array(
                        'route'     => '[:act[/:client]]', // as Sam suggested
                        'defaults'  => array(
                            'act' => 'login',
                            'client' => 'fsadfasf32454g43g43543',
                        ),
                        'constraints' => array(
                            'act' => '(login|logout|info)',
                            'client'    => '[a-zA-Z0-9]+',
                        ),
                    ),
                ),
            ),
        ),
    ),
),