ZF2 - url()查看助手问题

时间:2014-07-07 18:31:05

标签: php zend-framework2

我遇到Zend Framework 2方法问题$this->url()

这是我的代码:

Module.php

public function onBootstrap(MvcEvent $e)
{
    // You may not need to do this if you're doing it elsewhere in your
    // application
    $eventManager = $e->getApplication()->getEventManager();

    $eventManager->attach('route', function($event)
    {
        $sm = $event->getApplication()->getServiceManager();
        $config = $event->getApplication()->getServiceManager()->get('Configuration');
        $localesConfig = $config['locales'];
        $locale = $event->getRouteMatch()->getParam('locale');

        if (empty($locale)) {
            $locale = $localesConfig['default'];
        }

        $translator = $sm->get('translator');
        $translator->setLocale($locale);
        $session->locale = $locale;

    }, -10);

    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
}

module.config.php

return array(
'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Segment',
            'options' => array(
                'route'    => '/[:locale]',
                'constraints' => array(
                    'locale' => '[a-z]{2}(-[A-Z]{2}){0,1}'
                ),
                'defaults' => array(
                    'locale'     => 'en-US',
                    'controller' => 'Application\Controller\Index',
                    'action'     => 'index',
                ),
            ),
            'may_terminate' => true,
        ),
        'pricing' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'       => '[/:locale]/pricing',
                'constraints' => array(
                    'locale' => '[a-z]{2}(-[A-Z]{2}){0,1}',
                ),
                'defaults' => array(
                    'locale'     => 'en-US',
                    'controller'    => 'Application\Controller\Pricing',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
        ),
    ),
),

并在layout.phtml中:

<li><a href="<?php echo $this->url('home', array('locale' => $this->locale)); ?>">
  <?php echo $this->translate('Home');?></a></li>

在此方法中,将忽略参数区域设置,最终结果为:

http://www.contoso.com/home

我想有以下链接:

http://www.contoso.com/home/en-US

由于

0 个答案:

没有答案