Phalcon: ControllerBase逻辑在除index/index.volt之外的每个视图中工作


Phalcon: ControllerBase logic works in every view except the index/index.volt

我决定在我自己的服务器上推送一个示例,以便检查从我亲爱的"localhost"迁移到真实服务器时遇到的任何问题。从昨天开始我就面临着一个我解决不了的问题:

我正在使用session variable"语言",导致使用正确的message.php(在我的情况下是英语,法语或中文)。

我所有的控制器都从我的ControllerBase扩展,这个ControllerBase管理语言逻辑。

这个想法很简单:在每个视图中,我有三个标志(cn, fr和en)当用户点击其中一个标志时,当前页面(更一般地说,所有将被探索的页面)将更改为所需的语言。

在本地目录中,它工作得很好…而在服务器上,它实际上在每个视图中工作,除了索引视图:

索引将始终保持其原始语言(在第一次连接时为英语),如果在另一个视图中更改了语言,则将进行调整。但是,点击索引中的标志永远不会改变语言,尽管新url以/fr, /en/cn结尾)。

我想不出毛病在哪里。特别是因为没有特定的错误调用,因为它在我的本地存储库中像魅力一样工作。

以下是ControllerBase逻辑:

<?php
use Phalcon'Mvc'Controller;
class ControllerBase extends Controller
{
// Here I check if the language session is alredy defined and I load the desired message.php
protected function _getTranslation()
{
    if ($this->session->has("language")) {
        if (file_exists("messages/".$this->session->get("language").".php")) {
           require "messages/".$this->session->get("language").".php";
        } else {
           require "messages/en.php";
        }
    } else {
        require "messages/en.php";
    }       
    //Return a translation object
    return new 'Phalcon'Translate'Adapter'NativeArray(array(
       "content" => $messages
    ));
}
// Here I check if the first parameter or the second parameter is defining the language, if not I load the default english language
protected function beforeExecuteRoute($dispatcher) 
{
    if ($this->dispatcher->getParam(0) == "fr") {
        $this->session->set("language", "fr");
    } elseif ($this->dispatcher->getParam(0) == "en") {
        $this->session->set("language", "en");
    } elseif ($this->dispatcher->getParam(0) == "cn") {
        $this->session->set("language", "cn");
    } else {
        if ($this->dispatcher->getParam(1) == "fr") {
            $this->session->set("language", "fr");
        } elseif ($this->dispatcher->getParam(1) == "en") {
            $this->session->set("language", "en");
        } elseif ($this->dispatcher->getParam(1) == "cn") {
            $this->session->set("language", "cn");
        } else {
            if ($this->session->has("language")) {
                $this->session->set("language", $this->session->get("language"));
            } else {
                $this->session->set("language", "en");
            }
        }
    }
}
// Here the I define the url for each flag at every view loading
protected function afterExecuteRoute($dispatcher) 
{
    $this->view->setVar("t", $this->_getTranslation());
    if ($this->dispatcher->getParam(0)) {
        if ($this->dispatcher->getParam(0) == "fr" || $this->dispatcher->getParam(0) == "en" || $this->dispatcher->getParam(0) == "cn") {
            $this->view->fr = "/sebfct/" . $this->dispatcher->getControllerName() . "/" . $this->dispatcher->getActionName() . "/fr";
            $this->view->en = "/sebfct/" . $this->dispatcher->getControllerName() . "/" . $this->dispatcher->getActionName() . "/en";
            $this->view->cn = "/sebfct/" . $this->dispatcher->getControllerName() . "/" . $this->dispatcher->getActionName() . "/cn";
        } else {
            $this->view->fr = "/sebfct/" . $this->dispatcher->getControllerName() . "/" . $this->dispatcher->getActionName() . "/" . $this->dispatcher->getParam(0) . "/fr";
            $this->view->en = "/sebfct/" . $this->dispatcher->getControllerName() . "/" . $this->dispatcher->getActionName() . "/" . $this->dispatcher->getParam(0) . "/en";
            $this->view->cn = "/sebfct/" . $this->dispatcher->getControllerName() . "/" . $this->dispatcher->getActionName() . "/" . $this->dispatcher->getParam(0) . "/cn";
        }   
    } else {
        $this->view->fr = "/sebfct/" . $this->dispatcher->getControllerName() . "/" . $this->dispatcher->getActionName() . "/fr";
        $this->view->en = "/sebfct/" . $this->dispatcher->getControllerName() . "/" . $this->dispatcher->getActionName() . "/en";
        $this->view->cn = "/sebfct/" . $this->dispatcher->getControllerName() . "/" . $this->dispatcher->getActionName() . "/cn";
    }

}

}

下面是索引。Volt(从它扩展的每个视图,包括index/index. Volt)

<!DOCTYPE html>
<html>
    <head>
        <title>TITLE</title>
    </head>
    {{ stylesheet_link("css/base.css") }}
    {{ stylesheet_link("css/layout.css") }}
    {{ stylesheet_link("css/skeleton.css") }}
    {{ stylesheet_link("css/main.css") }}
    {{ stylesheet }}
    <body>
        <div class="container">
            <div class="one columns">
                <a class="nav-link" href="/sebfct">
                    {{ homeIcon }}
                </a>
            </div>
            <div class="two columns">
                <a class="nav-link" href="/sebfct">
                    <h4 class="nav-bar">WEBSITE</h1>
                    <h5>Version 1.2</h5>
                </a>
            </div>
            <div class="one column offset-by-ten nav-bar"><a href= {{ en }}>{{ english }}</a></div>
            <div class="one column nav-bar"><a href= {{ fr }}>{{ french }}</a></div>
            <div class="one column nav-bar"><a href= {{ cn }}>{{ chinese }}</a></div>
            <div class="sixteen columns">
            </div>
            <div class="three columns offset-by-ten menu">
                <h4><a class="nav-link" href="/sebfct/tutorial"><?php echo $t->_("gen_tuto") ?></a></h1>
            </div>
            <div class="three columns menu">
                <h4><a class="nav-link" href="/sebfct/about"><?php echo $t->_("gen_about") ?></a></h1>
            </div>
            <div class="sixteen columns">
                <hr />
            </div>
        </div>
        {{ content() }}
    </body>
</html>

所以正如我之前提到的,这个逻辑在每个视图中都工作得很好,除了index/index.volt,我的网站架构如下:

website
    .phalcon
    app
        cache
        config
        controller
            AboutController.php
            ControllerBase.php
            IndexController.php
            TutorialController.php
        models
        views
            about
                index.volt
            index
                index.volt
            tutorial
                index.volt
            index.volt /* This one is the one described above */
    public
        .... public things ....
    .htaccess
    index.html

任何建议都是欢迎的,即使它看起来微不足道。提前谢谢你

编辑:更精确的URL传递

通过标志传递的url是想要的(所以当我点击一个标志时,新的url在我的本地存储库和服务器中是完全相同的,除了"localhost"变成"X.X.X.X:PORT"。

以索引为例,url是localhost/sebfct (X.X.X.X:PORT/sebfct),点击法国标志将用户重定向到url localhost/sebfct/index/index/fr (X.X.X.X:PORT/sebfct/index/index/fr),请注意,在这种情况下,第一个"索引"是Controller,第二个是Action

如果有必要,我可以加入网站的url,但我真的不知道它在SO问题中是否被"接受",或者它是否甚至可能有用。

我不确定是否完全理解你的问题,但让我在这里试试。

首先,我假设你的公共文件夹中有index.php,用于配置你的设置和注入。

我注意到你的架构的问题是你不应该有一个索引。Volt在视图文件夹内的任何文件夹外。

如果您想从该文件(即。index。volt)然后你要把它放到一个文件夹里(你可以叫它templates)然后放

{% extends "templates/index.volt" %} 

放在你需要扩展格式的文件的顶部,如果你使用的是volt模板。

如果仍然有关于加载的问题,那么你可以尝试在你的公共文件夹中配置index.php的url设置等。

如果上面不是问题,那么你可以尝试使用node.js并设置一个服务器进行测试;或者你可以在system32中配置你的vhost设置,并配置apache设置以拥有一个基本url。