Search code examples
phpphalconvolt

Reload current page and upload a persistent variable in Phalcon


All my view in my current project extend from a general index view which looks like this:

<!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">
               <!-- HERE IS THE NAV-BAR -->
            </div>

            <div class="sixteen columns">
                <hr />
            </div>
            <div class="one column offset-by-thirteen"><a href="#">{{ english }}</a></div>
            <div class="one column"><a href="#">{{ french }}</a></div>
            <div class="one column"><a href="#">{{ chinese }}</a></div>
        </div>
        {{ content() }}
    </body>
</html>

So this general index is just providing a navigation bar and 3 links (english, french, chinese).

The controller looks like the following:

<?php

use Phalcon\Mvc\Controller;

class ControllerBase extends Controller
{

    protected function beforeExecuteRoute($dispatcher) 
    {

        $default_language = "en";

        if (!isset($this->persistent->lang)) {
            $this->persistent->lang = $default_language;
        }

        // PROVIDING DATA HERE SUCH LIKE PICTURES
    }

    protected function afterExecuteRoute($dispatcher) 
    {

        $this->view->url = $this->dispatcher->getControllerName() . "/" . $this->dispatcher->getActionName() . "/" . $this->dispatcher->getParams();

    }
}

In the protected function I try to get the current url.

My goal is to be able to change language by reloading the current url and updating a persistent variable:

$this->persistent->lang

(so by providing a parameter while reloading the page for example), I don't want to load the home page but the current page.

In my provided code, I am trying to get the desired url by calling:

$this->dispatcher->getControllerName() . "/" . $this->dispatcher->getActionName() . "/" . $this->dispatcher->getParams();

But the getParams() just give me a empty array...

For instance, I have a controller AboutController, which have the following action:

resumeAction($author)

So the url for this page is:

http://localhost/website/about/resume/bob

But the variable url defined in my BaseController give me about/resume/Array and not about/resume/bob.

How to get the desired path?


Solution

  • But the variable url defined in my BaseController give me about/resume/Array and not about/resume/bob.

    When you see Array show up it means that the variable is an array & you should access it as such. So $this->dispatcher->getParams() is a function that returns an array. Now you need to act on it. You can check it’s contents like this:

    echo '<pre>';
    print_r($this->dispatcher->getParams());
    echo '</pre>';
    

    And then you can view additional data by doing this:

    echo '<pre>';
    print_r($this->dispatcher->getParam('[name of the param here]'));
    echo '</pre>';
    

    And just change [name of the param here] to the actual name of the parameter you would like to access.

    More details on getting parameters up on the official Phalcon documentation.