DraugDev
DraugDev

Reputation: 81

Codeigniter and 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!

Upvotes: 0

Views: 49

Answers (1)

mdamia
mdamia

Reputation: 4557

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.

Upvotes: 1

Related Questions