我使用 CodeIgniter 编写站点...我知道 $_GET 请求现在像这样使用 www.website.com/function/value..在控制器中获取 URL 段是这样写的:
$userId = $this->uri->segment(3, 0);
我只是想知道,当控制器加载时,我想检查是否有任何 uri 段,如果有,则推送到一个视图,否则如果没有 uri 段推送到另一个视图。
这可能吗?
干杯。
您也可以为此使用控制器参数。
访问/user/profile/1 时,名为 User 的控制器将调用方法 profile() 并将数字 1 作为第一个参数传递给您的方法。这样:
class User extends CI_Controller {
{
public function index()
{
$this->load->view("user_index");
}
public function profile ( $userId = null )
{
if( (int)$userId > 0 )
$this->load->view("user_profile");
else
$this->load->view("another_view");
}
}
这是一个非常基本的示例,我只是想展示这个想法。
似乎你问了两个问题...
首先,检查请求是否得到
public function get_test()
{
if($_SERVER['REQUEST_METHOD'] == "GET")
{
//do something from get
echo "GET";
}
else
{
//do something not get
echo "NOT GET";
}
}
下一个问题似乎是检查 uri 段
public function get_test()
{
if($_SERVER['REQUEST_METHOD'] == "GET")
{
//do something from get
//echo "GET";
if($this->uri->segment(3)) //is true as is not empty
{
echo $this->uri->segment(3);
}
else
{
echo "I am nothing without my URI Segment";
}
}
else
{
//do something not get
echo "NOT GET";
}
}
据我了解,您可以使用PHP默认值。
function myFunction($var1 = NULL) {... if($var1 === NULL) ...}
现在,如果您不传递参数,您将获得 NULL 值。
我仍然没有使用代码点火器版本 2,但这个框架不接受 get 请求; 除非你弄乱了配置。有一个函数$this->input->get('myGet'),你应该环顾四周 de the codeigniter.com/user_guide