How to fetch value from url in silverstripe

孤者浪人 提交于 2019-12-08 04:18:26

问题


I want to print value 5 on the ss page.

www.xyz.com?a=5.

How to fetch url data in silverstripe? Any help is accepeted.


回答1:


In your controller that your Silverstripe template is for, you can retrieve "GET" (aka. query string) by returning the result of $this->getRequest()->getVar('a') in a function on your controller.

It is good practice to use $this->getRequest()->getVar('a') over $_GET['a'] as SilverStripe will automatically sanitise the string.

When your code is not in the controller (so you can't make use of $this->getRequest()), you can request the current controller by using Controller::curr() which will make the complete call for getting a single var:

Controller::curr()->getRequest()->getVar('a')

If you want to get all "GET" variables, just call getVars() instead..

Also, you can access "POST" variables in a similar calling postVar('a') or postVars() instead. If you want to get the value from both "POST" or "GET", you can call requestVar('a') or requestVars().

Anyway, here is a basic mock-up of a controller using a function on the controller that is accessible in the template.

Controller

class TestPage_Controller extends Page_Controller
{
    public function init()
    {
        parent::init();
    }

    public function MySpecialProperty()
    {
        return $this->getRequest()->getVar('a');
    }
}

Template

<p> $MySpecialProperty </p>


来源:https://stackoverflow.com/questions/28560679/how-to-fetch-value-from-url-in-silverstripe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!