Hello everyone,
I decided to push a sample in my own server in order to check any trouble while migrating from my dear "localhost" to a real server. I am facing a trouble I can't solve since yesterday:
I am using a session variable
"language" which lead the use of the correct message.php
(english, french or chinese in my case).
All my controllers extend from my ControllerBase
, and this ControllerBase
is managing the language logic.
The idea is simple: in every view I have three flag (cn, fr and en) when the user is clicking one of these flag, the current page (and more generally all the page which will be explored) change to the desired language.
In a local directory, it works pretty well... while on the server it is actually working in every view except the index view...
I can't figure out where is the trouble. Especially because there is no specific error called and because it works like a charm in my local repository.
The following is the ControllerBase
logic:
<?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";
}
}
}
The following is the index.volt (every view extend from it)
<!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>
So as I metioned before, this logic is working pretty well in every view except the index/index.volt, The architecture for my website is the following:
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
public
.... public things ....
.htaccess
index.html
Any advice would be welcome, even if it seems trivial. Thank you in advance