【问题标题】:CodeIgniter RESTful and controllerCodeIgniter RESTful 和控制器
【发布时间】:2012-07-30 10:20:29
【问题描述】:

您好,我是 CI 和 MVC 的新手,我正在尝试制作一个 RESTful 应用程序。

我已经阅读了很多(真的)并且我有以下规范

RESTful

读取(GET)

/object
/object.xml 
/object.json 

读取 ID (GET)

/object/id
/object/id.xml 
/object/id.json 

创建(POST)

/object
/object.xml 
/object.json 

更新(PUT)

/object/id
/object/id.xml 
/object/id.json 

删除(DELETE)

/object/id
/object/id.xml 
/object/id.json 

基于以上,当extension为.xml时返回xml,当.json返回json时,on extension返回html

当谈到 CI CRUD 时,我有以下网址

 /object
 /object/edit/id
 /odject/delete/id

我的问题是

我需要 2 个控制器,1 个用于 RESTful,1 个用于 CI CRUD,或者我只能有 1 个, 以及我怎样才能拥有多个 respesentation (html,xml,json)。

任何帮助appricated(阅读链接)

谢谢

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    看看:http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/

    你也可以用不同的方式来做这件事,但是我认为上面可能是最好的起点。

    更新

    另一种方式,可能是使用路线。

    $routes['object\.(xml|http|json)'] = 'process_object/$1';
    $routes['object/(:num)\.(xml|http|json)'] = 'process_object/$2/$1';
    $routes['object/crud/\.(xml|http|json)'] = 'process_object/$1/null/true';
    $routes['object/crud/(:num)\.(xml|http|json)'] = 'process_object/$2/$1/true';
    

    然后您的process_object 操作:

    function process_object($Format = 'xml', $ID = null, $CRUD = false)
    {
        $method = $this->_detect_method(); // see http://stackoverflow.com/questions/5540781/get-a-put-request-with-codeigniter
        $view = null;
        $data = array();
        switch($method)
        {
            case 'get' :
            {
                if($CRUD !== false)
                    $view = 'CRUD/Get';
                if($ID === null)
                {
                    // get a list
                    $data['Objects'] = $this->my_model->Get();
                }
                else
                {
                    $data['Objects'] = $this->my_model->GetById($ID);
                }
            }
            break;
            case 'put' :
            {
                if($CRUD !== false)
                    $view = 'CRUD/Put';
                $this->my_model->Update($ID, $_POST);
            }
            break;
            case 'post' :
            {
                if($CRUD !== false)
                    $view = 'CRUD/Post';
                $this->my_model->Insert($_POST);
            }
            break;
            case 'delete' :
            {
                if($CRUD !== false)
                    $view = 'CRUD/Delete';
                $this->my_model->Delete($ID);
            }
            break;
        }
        if($view != null)
        {
            $this->load->view($view, $data);
        }
        else
        {
            switch(strtolower($Format))
            {
                case 'xml' :
                {
                    // create and output XML based on $data.
                    header('content-type: application/xml');
                }
                break;
                case 'json' :
                {
                    // create and output JSON based on $data.
                    header('content-type: application/json');
                    echo json_encode($data);
                }
                break;
                case 'xml' :
                {
                    // create and output HTML based on $data.
                    header('content-type: text/html');
                }
                break;
            }
        }
    }
    

    注意:我还没有测试过这段代码,所以它需要工作。

    【讨论】:

    • 谢谢我已经看过了,但是这个有一个restfull控制器扩展而不是CI。所以我不清楚我是否必须有2个控制器,因为2个控制器是双重工作。谢谢跨度>
    • 您可以“潜在地”拥有一个控制器,但是这样做可能会使事情复杂化。我正在查看的另一种方式是使用 Routes。请参阅我的更新答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    相关资源
    最近更新 更多