0

I would like to know if it's possible out of the box in CodeIgniter2 to create the urls without modifying routes.php for each controller's function like this:

  1. backend.website.com/ecommerce/products/edit/$id
  2. backend.website.com/ecommerce/currencies/form/$id
  3. backend.website.com/ecommerce/customers/partners/etc/$arg1/$arg2

My controllers/ecommerce.php for function products() is something like this:

public function products($page = 0, $items = NULL, $subview = NULL)
{

    if($subview != NULL){
        // This function is localhost/ecommerce/products/form/$some_id
        _remap($subview); // gives me an error


    }else{
    // Default view with pagination arguments $page and $items

    // Page settings
    $this->config->set_item('page_name',    'Products');
    $this->config->set_item('page_subname', 'Product listing table');

    // Request items
    $this->data['items'] = $this->ecommerce_model->get_products(array('page' => $page, 'items' => $items));

    // Creating a view
    $this->data['view'] = $this->load->view('/ecommerce/products/index', $this->data, TRUE);
    $this->load->view('/_templates/default/index', $this->data);

    }
}

public function _remap($method)
{
    if ($method == 'form')
    {
        $this->$method();
    }
    else
    {
        // SKIP
    }
}

I found that default _remap() function could be useful, but I do not understand how to use it with my function.

Does anyone have any experience with that and could provide some little sample?

==== UPDATED ====

Is it even possible for _remap() to work with another functions such as orders(), customers(), currencies(), etc... at the same time?

aspirinemaga
  • 3,753
  • 10
  • 52
  • 95

1 Answers1

2

You don't need to complicate things that much for something like this.

Just add another parameter to each of your methods, for example $action:

public function products($action = false, $page = 0, $items = NULL, $subview = NULL) { 

    switch($action) {

        case 'edit':
            // edit stuff here
            break;

        case 'something else':
           // other stuff
           break;
    }
    // etc...
}
Shomz
  • 37,421
  • 4
  • 57
  • 85
  • please, could you explain, how to use it with CI pagination ? when i click on 2nd page, it will redirect to `/ecommerce/products/25`. Now 25 becomes `$action = 25`, and will ignore the rest. Is it possible to make it work with `$action` at the same time ? – aspirinemaga Dec 22 '13 at 16:55
  • I haven't used pagination for a while, let me get back to you in a few minutes... But it has to be possible to do it, I think it's something to do with url_segment. – Shomz Dec 22 '13 at 18:00
  • 1
    Yup, I was right, read more in the docs, but you should use `$config['uri_segment']`. 3 is the default value, you should go with 4... – Shomz Dec 22 '13 at 18:02
  • 1
    perfect i will try that out in a while! thank you very much Shomz! – aspirinemaga Dec 22 '13 at 18:19