Checking if a request is POST?

#1
[eluser]Unknown[/eluser]
How can I check if a request was submitted with the POST method (does CI offer a shorter way than "if($_SERVER['REQUEST_METHOD'] === 'POST')")?

#2
[eluser]sqwk[/eluser]
Code:
$something = $this->input->post('something');

http://ellislab.com/codeigniter/user-gui...input.html

#3
[eluser]skunkbad[/eluser]
[quote author="squawk" date="1266706903"]
Code:
$something = $this->input->post('something');

http://ellislab.com/codeigniter/user-gui...input.html[/quote]

That only checks for the existence of $_POST['something'], so it won't work.

#4
[eluser]heavenquake[/eluser]
I don't believe there is, but you could write a quick helper function that did it for you.

Personally I extend input->post() to, if no argument is given, return TRUE if anything has been posted at all. You might want to do something similar.

#5
[eluser]Unknown[/eluser]
How do you override controller methods, preserving the old one? Like in Ruby I'd do

Code:
class Controller
  alias_method :post_orig, :post

  def post *args
    if args.empty?
      # Code to determine if POST
    else
      post_orig args
    end
  end
end

Sorry for the example in another lang, my PHP is rusty.

#6
[eluser]heavenquake[/eluser]
input->post is not a controller method, but a method of the input library ( http://ellislab.com/codeigniter/user-gui...input.html ).

In CodeIgniter you extend core libraries (such as input) by placing a file called MY_Input.php in your libraries folder, with the following prototype:
Code:
class MY_Input extends CI_Input {

//your extended code here

}
while still calling it with $this->input->post() as you normally would. more on that here: http://ellislab.com/codeigniter/user-gui...asses.html

in your case it would be something alá:
Code:
class MY_Input extends CI_Input {

    function post($index = '', $xss_clean = FALSE)
    {
        // this will be true if post() is called without arguments
        if($index === '')
        {
            return ($_SERVER['REQUEST_METHOD'] === 'POST');
        }
        
        // otherwise do as normally
        return parent::post($index, $xss_clean);
    }
}

#7
[eluser]hugle[/eluser]
[quote author="heavenquake" date="1266739418"]input->post is not a controller method, but a method of the input library ( http://ellislab.com/codeigniter/user-gui...input.html ).

In CodeIgniter you extend core libraries (such as input) by placing a file called MY_Input.php in your libraries folder, with the following prototype:
Code:
class MY_Input extends CI_Input {

//your extended code here

}
while still calling it with $this->input->post() as you normally would. more on that here: http://ellislab.com/codeigniter/user-gui...asses.html

in your case it would be something alá:
Code:
class MY_Input extends CI_Input {

    function post($index = '', $xss_clean = FALSE)
    {
        // this will be true if post() is called without arguments
        if($index === '')
        {
            return ($_SERVER['REQUEST_METHOD'] === 'POST');
        }
        
        // otherwise do as normally
        return parent::post($index, $xss_clean);
    }
}
[/quote]

thank you very much for this extension,it's more like CI style.Smile

I used to use this:
Code:
if($_POST)
{
   // do something
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.