Zend Framework 2 - 路由转换,包括Controller和Action

时间:2014-07-16 01:42:11

标签: php zend-framework zend-framework2

我想知道如何在ZF2中翻译整个路线。我已经尝试过:Zend \ Mvc \ Router \ Http \ TranslatorAwareTreeRouteStack

它部分修复了我的问题,我仍然无法翻译动作或控制器,这就是我想知道的。

这就是我到目前为止,我可以翻译“{new}”,但不能翻译“:action”

        'news' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '/[:locale/]{news}[/page/:page][/:action[/:id]]',
                'defaults' => array(
                    'controller' => 'Application\Controller\News',
                    'action' => 'news'
                ),
            ),
        ),

traduction在一个名为“fr.php”的PHP文件中的数组中:

 return array(
    'news' => 'nouvelles',
    'details' => 'dd'
);

“详情”是我要翻译的行动。

我的模块课程:

namespace Application;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\Config\Reader\Xml as Xml;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);

    // Réglage de la langue
    $routeCallback = function ($e) {
        $availableLanguages = array ('fr', 'en');
        $defaultLanguage = 'fr';

        $fromRoute = false;
        //see if language could be find in url
        if ($e->getRouteMatch()->getParam('locale')) {
            $language = $e->getRouteMatch()->getParam('locale');
            $fromRoute = true;

            //or use language from http accept
        } else {
            $headers = $e->getApplication()->getRequest()->getHeaders();
            if ($headers->has('Accept-Language')) {
                $headerLocale = $headers->get('Accept-Language')->getPrioritized();
                $language = substr($headerLocale[0]->getLanguage(), 0,2);
            }
        }
        if(!in_array($language, $availableLanguages) ) {
            $e->getResponse()->setStatusCode(404);
            return;
        }
        $e->getApplication()->getServiceManager()->get('translator')->setLocale($language);

    };

    $eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_ROUTE, $routeCallback);

    // Traduction du routeur
    // Load translator
   $translator = $e->getApplication()->getServiceManager()->get('translator');
   // setup the translation file. you can use .mo files or whatever, check the translator api
   $language = (preg_match('/\/en\//', $_SERVER["REQUEST_URI"])) ?'en' :'fr';
   $translator->addTranslationFile('PhpArray', __DIR__.'/language/fr.php', 'default', $language); 
   $app = $e->getTarget();

   // Route translator
   $app->getEventManager()->attach('route', array($this, 'onPreRoute'), 100);

   // Application Configuration
    $serviceManager = $e->getApplication()->getServiceManager();
    $viewModel = $e->getApplication()->getMvcEvent()->getViewModel();
    $reader = new Xml();
    $appConfig   = $reader->fromFile('public/ApplicationConfiguration.xml');
    $viewModel->appConfig = $appConfig;

    $viewModel->translator = $e->getApplication()->getServiceManager()->get('translator');

}

public function onPreRoute($e){
    $app      = $e->getTarget();
    $serviceManager       = $app->getServiceManager();
    $serviceManager->get('router')->setTranslator($serviceManager->get('translator'));
}

public function getConfig()
{
    return include __DIR__ . '/config/module.config.php';
}

public function getAutoloaderConfig()
{
    return array(
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
            ),
        ),
    );
}

}

如何翻译控制器和操作?

2 个答案:

答案 0 :(得分:0)

万一你找不到合适的解决方案,我这样做的方法是定义一个像

这样的路线
    'news_fr' => array(
        'type' => 'Segment',
        'options' => array(
            'route' => '/[:locale/]{news}[/page/:page][/:action[/:id]]',
            'defaults' => array(
                'controller' => 'Application\Controller\News',
                'action' => 'notice'
            ),
        ),
    ),


    'news_eng' => array(
        'type' => 'Segment',
        'options' => array(
            'route' => '/[:locale/]{news}[/page/:page][/:action[/:id]]',
            'defaults' => array(
                'controller' => 'Application\Controller\News',
                'action' => 'news'
            ),
        ),
    ),

然后在视图/布局上我做

<?= $this->url('news_'.$currentLanguage, array('par1',...)); ?>

答案 1 :(得分:0)

很简单,在路线配置

return array(
    'router' => array(
        'router_class' => 'Zend\Mvc\Router\Http\TranslatorAwareTreeRouteStack',
        'routes' => array(...)
    ),
    ...
);

在Module.php的函数onBootstrap

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $eventManager->attach('route', array($this, 'onPreRoute'), 100);
        $moduleRouteListener->attach($eventManager);
    }
    public function onPreRoute($e)
    {
        $app = $e->getTarget();
        $serviceManager = $app->getServiceManager();
        $serviceManager->get('router')->setTranslator($serviceManager->get('translator'));
    }
    ...
}