We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Session variable work in every view except the index

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

An ugly but effective method would be to put exit() statements at various places in your ControllerBase file. Then, when you view your index page, see if all the methods are being called that you expect. My guess is that the index controller is reaching a condition none of the other controllers are.

Also, possibly silly question but it must be asked: Is your IndexController actually extending ControllerBase?



1.7k

(@quasipickle I reply here, the icon reply close to downgrade and upgrade call nothing)

First thank you for your answer. I will be a little busy today, so I will try your solution as soon as possible (probably after my work).

I checked (just for being sure) but yes, of course the indexController extends ControllerBase (without it, the flag wouldn't even be displayed on the screen since their variable giving the url for the .png file wouldn't be loaded).

I am also wondering about your comment:

the index controller is reaching a condition none of the other controllers are.

how this none condition can be reached in the server but not in localhost? (both repository in my local machine and my server are exactly the same).

Again, thank you for your help, I will give some news ASAP. (I can also join the url of the server if needed, I am just not sure it is tolerated, or it would be useful).

Sincerely,



1.7k

@quasipickle After testing your method it appears that everything is called well (by everything please refer to the above urnordered list).

I created a new server, I installed phalcon in the same configuration as my local machine, the result is the same:

In localhost, the language are displayed well in every view, while in the distant server, the language is displayed everywhere except the index view. Everything seems to be called correctly:

  • The flag are well displayed (their url are loaded by the BaseController)
  • The flag are pointing to the correct url: the current one + /fr, /en or /cn (also loaded by the BaseController)
  • The current url are always the one desired.

If anyone have any idea, please don't hesitate! (And if someone have time for testing my code source it would be also helpful for me even if I am afraid that everything "work" well in localhost...

Sincerely,



1.7k
edited Jun '14

Hi everyone,

I would like to add some of my research:

When I save my parameter $this->view->url = $this->dispatcher->getParam(0); in my ControllerBase.php, and then I print it in my index.volt: {{ url }}, I obtain the following:

In my localhost:

  1. page index : nothing printed
  2. page index/index/fr : fr printed
  3. page index/index/en : en printed
  4. page index/index/cn : cn printed

In my distant server: nothing is printed with {{ url }} in all the cases.

Once again, the installation and the file are totally the same, and I reinstalled a new server in order to be sure of it.

Sincerely,