简体   繁体   中英

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

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:

The index will always keep its original language (english at the first connection, and then will be adapted if you change the language in the other view. But clicking a flag in the the index will never change the language, despite the new url with /fr , /en or /cn at the end).

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, including the 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>

So as I mentionned 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
            index.volt /* This one is the one described above */
    public
        .... public things ....
    .htaccess
    index.html

Any advice would be welcome, even if it seems trivial. Thank you in advance

EDIT: More precision about the URL passed

The url passed by the flag are the one desired (so when I click on a flag, the new url is exactly the same in my local repository and my server except that "localhost" become "XXXX:PORT".

In the case of the index for instance, the url is localhost/sebfct ( XXXX:PORT/sebfct ) and a click on the french flag will redirect the user to the url localhost/sebfct/index/index/fr ( XXXX:PORT/sebfct/index/index/fr ), note that in this case the first "index" is the Controller and the second one is the Action .

I could join the url of the website if necessary, but I do not really know if it is "accepted" in SO question or if it could even be useful.

I am not sure if I understand your question completely, but let me try here.

First of all, I would assume that you have your index.php inside your public folder for configuring your settings and injections.

Problem I notice on your architecture is that you shouldn't have a index.volt outside of any folder inside the views folder.

If you want to extend from that file (ie. index.volt) then you have to put it inside a folder (you can call it "templates") and then put

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

at the top of whichever file you need to be extended regarding formats and such if you are using volt template.

If there is still problem about loading and such then you can try to configure your index.php inside your public folder for url settings and such.

If the above is not the problem then you can try using node.js and set up a server for testing; or you can configure your vhost setting inside your system32 and configure apache settings to have a base url.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM