Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Zend Translate+;自定义url_Php_Zend Framework_Multilingual - Fatal编程技术网

Php Zend Translate+;自定义url

Php Zend Translate+;自定义url,php,zend-framework,multilingual,Php,Zend Framework,Multilingual,这是我的引导文件中的内容 /* * Here the Translator is enabled and passed to * Zend_Registry so can be accesed from anywhere */ public static function translateOption() { Zend_Loader::loadClass('Zend_Translate'); $translate = new Zend_Translate(

这是我的引导文件中的内容

/*
* Here the Translator is enabled and passed to
* Zend_Registry so can be accesed from anywhere
*/
public static function translateOption()
{
    Zend_Loader::loadClass('Zend_Translate');
    $translate = new Zend_Translate(
            'Zend_Translate_Adapter_Csv', 
            self::$root."/lang/en/language.csv", 
            'en');
    self::$registry->translate = $translate;
}
我试图获得的是一个类似mysite.com/language/controller/action的URL

这是我的路线

;Multilang routes
lang_default.type                                = "iM_Controller_Router_Route_Language"
lang_default.route               = ":language/:controller/:action/*"
lang_default.defaults.module     = default
lang_default.defaults.controller = index
lang_default.defaults.action     = index
lang_default.defaults.language   = en
lang_default.reqs.language       = "(ro|en)"
这是我在互联网上找到的路线控制器插件:

<?php

/**
* Front Controller Plugin; Created by Gabriel Solomon (http://www.gsdesign.ro/blog/optimizing-zend-routing/)
*
* @uses       Zend_Controller_Plugin_Abstract
* @category   iM
* @package    iM_Controller
* @subpackage Plugins
*/
class iM_Controller_Plugin_Router extends Zend_Controller_Plugin_Abstract{
    protected $_dir;
    protected $_default = array();
    protected $_request;

    protected $_initialConfig;
    protected $_remainingConfig;


    public function routeStartup(Zend_Controller_Request_Abstract $request)
    {
        // define some routes (URLs)
        $router = Zend_Controller_Front::getInstance()->getRouter();       
        $this->setRequest($request);       

        $config = $this->getInitial();
        $router->addConfig($config);
    }


    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $router = Zend_Controller_Front::getInstance()->getRouter();

        $config = $this->getRemaining();

        $router->addConfig($config);
    }

    public function setDir($dir) {
        $this->_dir = $dir;
    }


    public function setDefault($default) {
        if (is_array($default)) {
            $this->_default = array_merge($this->_default, $default);
        } else {
            $this->_default[] = $default;
        }
    }


    public function setRequest(Zend_Controller_Request_Abstract $request) {
        $this->_request = $request;
        //return $this;
    }

    public function getInitial() {
        if ($this->_initialConfig == null) {
            $this->parseIniDir();
        }

        return $this->_initialConfig;

    }


    public function getRemaining() {
        if ($this->_remainingConfig == null) {
            $this->parseIniDir();
        }

        return $this->_remainingConfig;
    }

    protected function parseIniDir() {
        $files = $this->getFiles();
        $this->_default;

        $this->_default[] = $this->determineInitial();

        $this->_initialConfig = new Zend_Config(array(), true);
        $this->_remainingConfig = new Zend_Config(array(), true);       

        if (is_array($files)) {

            foreach ($files as $file) {
                $routerFile = $this->compilePath($file);

                if (in_array($file, $this->_default)) {
                    $this->_initialConfig->merge(new Zend_Config_Ini($routerFile));

                } else {
                    $this->_remainingConfig->merge(new Zend_Config_Ini($routerFile));
                }
            }
        }
    }

    protected function getFiles() {
        if (is_dir($this->_dir)) {
            $dir = new DirectoryIterator($this->_dir);
            $files = array();
            foreach($dir as $fileInfo) {
                if(!$fileInfo->isDot() && $fileInfo->isFile()) {
                    $files[] = $fileInfo->__toString();
                }
            }

            return $files;
        }

        return false;
    }


    protected function getOtherRoutes() {
        $routes->merge(new Zend_Config_Ini($routerFile));
    }


    protected function determineInitial() {
        if ($this->_request) {
            $uri = $this->_request->getRequestUri();
            $base = $this->_request->getBasePath() . '/';

            $request = str_replace($base, '', $uri);
            $requestParts = explode('/', $request);

            $lang = $requestParts[0];
            (array_key_exists(1,$requestParts) ? $section = $requestParts[1] : $section = '');

            if (!empty($section) && $section == 'user') {
                return 'user.ini';
            }

        }
        return false;
    }


    protected function compilePath($file) {
        return $this->_dir . '/' . $file;
    }
}

在引导过程中,应用程序尚未“知道”URL中的内容。引导用于准备应用程序,然后使其运行。因此,在引导过程之后,dispatchloop就开始了。在路由过程中解析URL,然后在解析URL时,调度进程调用与路由中的详细信息相关联的模块/控制器/操作

与其在引导程序中阅读和配置翻译,我建议在控制器插件中这样做。在dispatchloop的各个步骤中,调用每个控制器插件中的某些方法

以下是一个示例:

class Application_Model_Language extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(\Zend_Controller_Request_Abstract $request)
    {
        // ask the language from the $request object
        // load the translation data
    }
}
然后,您所要做的就是告诉Zend_应用程序您想在dispatchloop中添加一个插件。这是在引导过程中完成的。因此,您可以将其放在通用Bootstrap.php或特定于模块的Bootstrap.php中:

protected function _initTranslations()
{
    $this->bootstrap('frontController');
    $front = $this->getResource('frontController');
    $front->registerPlugin(new Application_Model_Language());
}