[英]Phalcon: ControllerBase logic works in every view except the index/index.volt
我决定在自己的服务器中推送一个示例,以检查从亲爱的“ localhost”迁移到真实服务器时遇到的任何问题。 自昨天以来,我面临着无法解决的麻烦:
我正在使用session variable
“语言”,导致使用正确的message.php
(在我的情况下为英语,法语或中文)。
我所有的控制器都从ControllerBase
扩展而来,该ControllerBase
正在管理语言逻辑。
这个想法很简单:在每个视图中,当用户单击其中一个标志时,我都有三个标志(cn,fr和en),当前页面(通常是将要浏览的所有页面)都将更改为所需的语言。
在本地目录中,它工作得很好……而在服务器上,它实际上在除索引视图之外的所有视图中都可以工作:
索引将始终保持其原始语言 (在第一个连接处为英语,然后在其他视图中更改语言时将进行调整。但是,尽管使用/fr
的新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";
}
}
}
以下是index.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与本地存储库和服务器中的URL完全相同,只不过“ localhost”变为“ XXXX:PORT”。
例如,在使用索引的情况下,URL为localhost/sebfct
( XXXX:PORT/sebfct
),然后单击法国标志将用户重定向到url localhost/sebfct/index/index/fr
( XXXX:PORT/sebfct/index/index/fr
),请注意,在这种情况下,第一个“索引”是Controller
,第二个是Action
。
如有必要,我可以加入网站的网址,但我真的不知道它是否在问题中被“接受”,甚至是否有用。
我不确定我是否完全理解您的问题,但是让我在这里尝试。
首先,我假设您在公用文件夹中有index.php,用于配置设置和注入。
我在您的体系结构上注意到的问题是,views文件夹内的任何文件夹都不应包含index.volt。
如果要从该文件扩展(即index.volt),则必须将其放在文件夹中(可以将其称为“模板”),然后放入
{% extends "templates/index.volt" %}
在任何文件的顶部,您都需要扩展格式,例如,如果您使用的是volt模板。
如果仍然存在关于加载之类的问题,那么您可以尝试在公用文件夹内配置index.php以进行url设置等。
如果上述不是问题,那么您可以尝试使用node.js并设置服务器进行测试; 或者,您可以在system32内配置虚拟主机设置,并配置apache设置以具有基本URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.