开发者

Codeigniter request control class

i'm trying to do myself a good simple request control library.

this is my code:

class CI_Request
{
    public function isAjax()
    {    
        return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
    }

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

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

but i don't know if i'm right.

any suggesti开发者_StackOverflow社区ons? :P

really thanks


Firstly your question is very vague.

You need to first understand tha Helpers and Libraries are two separate entities within the framework

Helpers, as the name suggests, help you with tasks. Each helper file is simply a collection of functions in a particular category. There are URL Helpers, that assist in creating links, there are Form Helpers that help you create form elements, Text Helpers perform various text formatting routines, Cookie Helpers set and read cookies, File Helpers help you deal with files, etc.

And libraries is usually a class thats a collection of methods to handle a specific task, witch is why i think libraries are what your looking for.

You can create a library file within application/libraries of your application directory and create a file called MyRequest.php, the contents of that file would be like so..

class CI_MyRequest
{
   //..
}

The file name and the class name are relative so they must be the same, loading the library from a controller is simple

class Index extends Controller
{
    public function __construct()
    {
        $this->library->load('MyRequest');
    }

    public function index()
    {
        if($this->MyRequest->isAjax())
        {
            //.. Send me some json.
        }
    }
}

Note: haven't touched CI for long time so code may not be exact.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜