duaevb1511 2015-08-21 22:46
浏览 35

Codeigniter和Rest for Gallery

I have to delete some images from Gallery (using MySQL with ID, URL, hidden and ALT columns). I want to do it with Rest. I don't realy understand how to get them work togther.

Here is my gallery controller:

public function delete_image($id) {
  // Load Model
  $this->load->model('gallery_model');
  // get delete function
  $this->gallery_model->delete_image($id);
}

Here is my gallery model:

// Delete image
function delete_image($id) {
  $this->db->delete('gallery', array('id' => $id));
}

UPD: Here is my Rest controller:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

require APPPATH.'/libraries/Rest.php';

class Rest_Gallery extends Rest {

  public function __construct() {
   parent::__construct();
  }

  public function delete_image() {
    // get the id to delete
    $id = $this -> uri ->segment(2);
    // Load Model
    $this->load->model('gallery_model');
    // get delete function
    $this->gallery_model->delete_image($id); 
    // return example 
    $this->response('OK', 200);
  }
}

UPD: Here is my default and backend gallery route to show images and to delete:

$route['backend/gallery'] = 'backend/gallery/index';
$route['backend/gallery/delete/:any'] = 'rest_gallery/image_delete';

$route['default_controller'] = 'pages/index';

But now I have a problem: What's next? How do I have to route all them together to get it work.

If you need more information, im glad to give it to you.

Thanks for help!

  • 写回答

1条回答 默认 最新

  • doutangkao2789 2015-08-21 22:53
    关注

    You don't need 2 controllers, let your rest contoller initiate the model and call the delete funtion. change this

    public function delete_image($id) {
       $this->response(array('returned from delete:' => $id, 200));
    }
    

    to

    public function delete_image() {
      $id = $app_name = $this -> uri -> segment(2);
      // Load Model
      $this->load->model('gallery_model');
      // get delete function
      $this->gallery_model->delete_image($id); 
      // return example 
      return ['message' => 'photo deleted'];
    
    }
    

    in your routes files add this

     $route['backend/gallery/delete/:any'] = 'you rest controller/method name';
    

    hope this help.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题