0

I am trying my routing works but its failing to route properly.

when i am going to www.example.com/nl/over-ons it fails to go to class AboutController extends AbstractActionController { public function nlAction() {}

// www.example.com/nl/over-ons fails
$route = Http\Segment::factory(array(
  'route' => '/nl/over-ons',
  'defaults' => array(
    'controller' => 'Application\Controller\About',
    'action' => 'nl'
  ),
));
$router->addRoute('nlaboutus', $route, null);  

// it works - www.example.com/nl
$route = Http\Segment::factory(array(
  'route' => '/nl',
  'defaults' => array(
    'controller' => 'Application\Controller\Nlindex',
    'action' => 'index'
  ),
));
$router->addRoute('nlindex', $route, null); 

3 Answers3

0

I don't think you can handle locale that way. If your app is nl only then construct a prefix of all your routes.

For doing this you can easily do that with 2 triggers in module.php

$eventManager->attach(
        MvcEvent::EVENT_ROUTE,
        array($this, 'setBaseUrl'),
        -100
    );

    // Trigger before 404s are rendered.
    $eventManager->attach(
        MvcEvent::EVENT_RENDER,
        array($this, 'setBaseUrl'),
        -1000
    );

Where setBaseUrl is a method

/**
 * Triggered after route matching to set the base URL for assembling with ProxyPass.
 *
 * @param \Zend\Mvc\MvcEvent $e
 */
public function setBaseUrl(MvcEvent $e)
{
    $router = $e->getApplication()->getServiceManager()->get('Router');
    $router->setBaseUrl('/nl');
}

If not refers to this question And this pull request

To resume : you can add a locale to your route.

Community
  • 1
  • 1
Greco Jonathan
  • 2,517
  • 2
  • 29
  • 54
0

You can also set the route /nl as Literal route and all other as child_routes. To learn more, see http://framework.zend.com/manual/current/en/modules/zend.mvc.routing.html

Alain Pomirol
  • 828
  • 7
  • 14
  • What do you mean by child_routers? `Http\Segment::factory`? –  Nov 30 '15 at 18:49
  • I set the /nl as Literal route, and /nl/others as Segment but its still same. –  Nov 30 '15 at 18:51
0

You can use a normal route config and use a factory to create the router. I think in this case your config would look like this:

use Zend\Mvc\Router\Http\TreeRouteStack as HttpRouter;

//...

$config = array(
    'routes' => array(
        'nlindex' => array(
            'type' => 'literal',
            'options' => array(
                'route' => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\NL\Index',
                    'action' => 'index'
                )
            )
            'may_terminate' => true,
            'child_routes' => array(
                'nlaboutus' => array(
                    'type' => 'literal',
                    'options' => array(
                        'route' => '/over-ons',
                        'defaults' => array(
                            'controller' => 'Application\Controller\About',
                            'action' => 'nl'
                        )
                    )
                )
            )
        )
    )
);

$router = HttpRouter::factory($config);
Wilt
  • 41,477
  • 12
  • 152
  • 203