[英]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.