开发者

Codeigniter - Checking if a GET request is made

开发者 https://www.devze.com 2023-03-08 09:48 出处:网络
Im using CodeIgniter to write a site ... I understand $_GET requests are now used like so www.website.com/fu开发者_运维问答nction/value .. and in the controller getting a url segment is written like s

Im using CodeIgniter to write a site ... I understand $_GET requests are now used like so www.website.com/fu开发者_运维问答nction/value .. and in the controller getting a url segment is written like so:

$userId = $this->uri->segment(3, 0);

Im just wondering, when a controller loads, i want to check if there is any uri segments, if there is then push to one view, else if there isnt a uri segment push to another.

Is that possible?

cheers.


You can use your controller arguments for that too.

When accessing /user/profile/1 your controller named User will call the method profile() and pass the number 1 as the first argument to your method. Like so:

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");
    }

}

This is a very basic sample and I'm just trying to show the idea.


Seems like your asking two questions...

First, to check if the request is get


public function get_test()
{
    if($_SERVER['REQUEST_METHOD'] == "GET")
    {
        //do something from get
        echo "GET";
    }
    else
    {
        //do something not get
        echo "NOT GET";
    }   
}

The next question seemed to be checking uri segments


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";
    }   
}


As I understand you can use PHP default value.

function myFunction($var1 = NULL) {... if($var1 === NULL) ...}

Now if you do not pass the param you will get the NULL value.


I am still not using version 2 of codeigniter but this framework do not accept get requests; unless you mess with the configuration. Theres a function $this->input->get('myGet') you should look around at de the codeigniter.com/user_guide

0

精彩评论

暂无评论...
验证码 换一张
取 消