thinkphp如何传递GET参数方法详解

来自:网络
时间:2023-07-25
阅读:

正文

在ThinkPHP中,我们可以利用URL地址来传递参数。ThinkPHP 框架会自动解析 URL 地址中的参数,并将其传递给相应的控制器和方法。

例如,我们的 URL 地址为:http://localhost/index.php/Index/index?id=1&name=thinkphp,其中 id=1 和 name=thinkphp 即为传递的参数。在控制器中,我们可以使用 $this->request->param() 方法来获取 URL 地址中传递的参数。例如:

<code><code>public function index()
{
    $id = $this->request->param('id');
    $name = $this->request->param('name');
    echo 'ID=' . $id . ', Name=' . $name;
}</code></code>

这样,当我们访问上述的 URL 地址时,控制器会输出:ID=1, Name=thinkphp

除了 URL 地址传递参数外,我们也可以使用表单来传递参数。在 HTML 表单中,我们可以使用 name 属性来标识需要传递的参数,而在控制器中同样可以使用 $this->request->param() 方法来获取表单中传递的参数。

例如,在 HTML 表单中,我们需要传递 id 和 name 参数。则可以这样编写 HTML 代码:

<form action="/index.php/Index/index" method="get">
    <input type="text" name="id" value="1">
    <input type="text" name="name" value="thinkphp">
    <input type="submit" value="提交">
</form>

在控制器中,我们同样可以使用 $this->request->param() 方法来获取表单中传递的参数。例如:

<code><code>public function index()
{
    $id = $this->request->param('id');
    $name = $this->request->param('name');
    echo 'ID=' . $id . ', Name=' . $name;
}</code></code>

这样,当我们提交表单后,控制器同样会输出:ID=1, Name=thinkphp

以上就是thinkphp如何传递GET参数的详细内容,更多关于thinkphp传递GET参数的资料请关注其它相关文章!

返回顶部
顶部