Welcome Guest, Not a member yet? Register   Sign In
My Request helper or library
#1

[eluser]Ngulo[/eluser]
hi all i'm trying to build up my simple personal request_helper just to verify if requests
isPost() or isGet() or isAjax()
i named my helper as request_helper and i load it by default in the autoload class.

i'm trying coding it so:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');



    function isGet($_GET)
        {
            if($_GET)
            {
                return isset($_GET);
            }
        }
        function isPost($_POST)
        {
            if($_POST)
            {
                return isset($_POST);
            }
        }
        function isAjax($_SERVER)
        {
            if($_SERVER['HTTP_X_REQUESTED_WITH'])
            {
                return isset($_SERVER['HTTP_X_REQUESTED_WITH']) == 'XMLHttpRequest';
            }
        }

when i make
Code:
if($this->request->isPost())
                {
                    echo 'yes is post';
                }

the result is
Code:
Call to a member function isPost() on a non-object

have you any suggestion guys Sad ?
#2

[eluser]slowgary[/eluser]
Did you load the helper?
Code:
$this->load->helper('request');

Also, I believe your methods are incorrect (I could be wrong). You should be able to use this:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');



    function isGet()
    {
         return isset($_GET);
    }

     function isPost()
     {
          return isset($_POST);
     }

     function isAjax()
     {
          return isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
     }
#3

[eluser]Ngulo[/eluser]
hi slowgary .... the helper is loaded by default from the system....a put them into autoload.php array helpers Wink

i can't really don't why i can't use these methods Sad

have you any other suggestion?
#4

[eluser]slowgary[/eluser]
I recommend reading through the user guide when you have a specific problem like this. Here's the reference for helpers:
http://ellislab.com/codeigniter/user-gui...lpers.html

When you load a helper, you're just including a file. There's no magic that will take your functions and make them methods of an object. This mean that when you use your helper functions, you'll just call them like normal function, not methods of an object:
Code:
$this->load->helper('request');

if(isPost())
{
     echo 'there is post data';
}
This is wrong:
Code:
$this->request->isPost();

I hope this helps.
#5

[eluser]Ngulo[/eluser]
oh my god ....you're right man .... Wink

and if would like to use $this->request->isPost(); need i to transform my helper into a library?

really thanks man Wink
#6

[eluser]slowgary[/eluser]
Exactly. Libraries are for classes, helpers and plugins are just procedural functions. Create a class with your functions, stick it in the application/libraries directory and load it with $this->load->library('request'), then you can call $this->request->isPost().

Just a recommendation, CodeIgniter uses_underscores as opposed to CamelCase. For consistency, you might want to consider naming your functions is_post(), is_get() and is_ajax().
#7

[eluser]Ngulo[/eluser]
thank you slowgary

Code:
<?php  
if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class CI_Request extends Controller {

    public function is_ajax() {


        return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
    }

    public function is_post(){

        return isset($_POST);
    }

    public function is_get(){

        return isset($_GET);
    }
    
}

?>
in my controller i declare like this:
Code:
$this->load->library('request');
                
                if($this->request->is_get())
                {
                    echo 'yes is get';
                }
                else{echo 'is not get';}
but it's still not working again Sad

did you find some other errors please ? Sad

really really thanks Wink
#8

[eluser]slowgary[/eluser]
This is covered in the user guide:
http://ellislab.com/codeigniter/user-gui...aries.html

Code:
class CI_Request extends Controller

That's how you write a controller, not a library. Check out the user guide for more info.

Good luck.
#9

[eluser]InsiteFX[/eluser]
Also CI_ is a CodeIgniter resevered word and should not
be used in your Controllers Libraries etc.

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB