如何从 silverstripe 中的 url 获取值
How to fetch value from url in silverstripe
我想在 ss 页面上打印值 5。
www.xyz.com?a=5.
如何在 silverstripe 中获取 url 数据?接受任何帮助。
在您的 Silverstripe 模板所对应的控制器中,您可以通过在控制器上的函数中返回 $this->getRequest()->getVar('a')
的结果来检索 "GET"(也称为查询字符串)。
最好使用 $this->getRequest()->getVar('a')
而不是 $_GET['a']
,因为 SilverStripe 会自动清理字符串。
当您的代码不在控制器中时(因此您无法使用 $this->getRequest()
),您可以 request the current controller by using Controller::curr()
这将完成获取单个变量的调用:
Controller::curr()->getRequest()->getVar('a')
如果要获取所有"GET"个变量,just call getVars()
instead..
此外,您可以在类似的调用中访问 "POST" 个变量 postVar('a')
or postVars()
instead. If you want to get the value from both "POST" or "GET", you can call requestVar('a')
or requestVars()
。
无论如何,这是一个控制器的基本模型,它使用了可在模板中访问的控制器功能。
控制器
class TestPage_Controller extends Page_Controller
{
public function init()
{
parent::init();
}
public function MySpecialProperty()
{
return $this->getRequest()->getVar('a');
}
}
模板
<p> $MySpecialProperty </p>
我想在 ss 页面上打印值 5。
www.xyz.com?a=5.
如何在 silverstripe 中获取 url 数据?接受任何帮助。
在您的 Silverstripe 模板所对应的控制器中,您可以通过在控制器上的函数中返回 $this->getRequest()->getVar('a')
的结果来检索 "GET"(也称为查询字符串)。
最好使用 $this->getRequest()->getVar('a')
而不是 $_GET['a']
,因为 SilverStripe 会自动清理字符串。
当您的代码不在控制器中时(因此您无法使用 $this->getRequest()
),您可以 request the current controller by using Controller::curr()
这将完成获取单个变量的调用:
Controller::curr()->getRequest()->getVar('a')
如果要获取所有"GET"个变量,just call getVars()
instead..
此外,您可以在类似的调用中访问 "POST" 个变量 postVar('a')
or postVars()
instead. If you want to get the value from both "POST" or "GET", you can call requestVar('a')
or requestVars()
。
无论如何,这是一个控制器的基本模型,它使用了可在模板中访问的控制器功能。
控制器
class TestPage_Controller extends Page_Controller
{
public function init()
{
parent::init();
}
public function MySpecialProperty()
{
return $this->getRequest()->getVar('a');
}
}
模板
<p> $MySpecialProperty </p>