重新加载当前页面并在Phalcon中上传持久变量

时间:2014-06-08 04:54:01

标签: php phalcon 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">
               <!-- 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>

所以这个一般索引只是提供一个导航栏和3个链接(英语,法语,中文)。

控制器如下所示:

<?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();

    }
}

在受保护的功能中,我尝试获取当前的URL。

我的目标是能够通过重新加载当前URL并更新持久变量来更改语言:

$this->persistent->lang

(例如,通过在重新加载页面时提供参数),我不想加载主页而是加载当前页面。

在我提供的代码中,我试图通过调用来获取所需的URL:

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

getParams()只给我一个空数组......

例如,我有一个控制器AboutController,它具有以下操作:

resumeAction($author)

所以这个页面的网址是:

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

但我url中定义的变量BaseController给了我about/resume/Array而不是about/resume/bob

如何获得所需的路径?

1 个答案:

答案 0 :(得分:0)

  

但我url中定义的变量BaseController给了我   about/resume/Array而非about/resume/bob

当您看到Array显示时,表示该变量是一个阵列&amp;你应该这样访问它。所以$this->dispatcher->getParams()是一个返回数组的函数。现在你需要采取行动。您可以检查它的内容如下:

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

然后您可以通过以下方式查看其他数据:

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

只需将[name of the param here]更改为您要访问的参数的实际名称。

有关在the official Phalcon documentation上获取参数的更多详细信息。