5

I have a function called "insert" in my categories controller. When I call the function via url like this: /categories/insert it works OK, but if I call the function like this: /categories/insert/ (slash at the end) the function is called three times.

Even when a call my edit function like this: /categories/edit/2 - the edit function is called three times.

In config/routes.php I only have default route. My .htaccess is this:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|include|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]  

EDIT:

The code for the edit function:

public function edit($id = '') 
{
    $this->load->helper("form");
    $this->load->library("form_validation");
    $data["title"] = "Edit category";

    $this->form_validation->set_rules('category_name', 'Category name', 'required');

    if (!$this->form_validation->run())
    {
        $data['category'] = $this->categories_model->get_categories($id);
        $this->load->view("templates/admin_header", $data);
        $this->load->view("categories/edit", $data);
        $this->load->view("templates/admin_footer", $data); 
    }
    else
    {
        $this->categories_model->update($id);
        // other logic
    }
}
Andrej
  • 415
  • 1
  • 7
  • 25
  • How can you say its called three times? can you post your code? – Sudz May 10 '13 at 14:24
  • I know, because I have a breakpoint on the first line of the code. I edited my original post with the code of the function. I don't think that it has anything to do with the code, because when I call the insert function without the slash at the end of the url, it works fine. – Andrej May 10 '13 at 15:00
  • What does your route look like? – Dawson May 10 '13 at 17:55

1 Answers1

1

** Edit ** http://your.dot.com/insert calls public function insert($arg) without data for $arg. http://your.dot.com/insert/ calls insert with 'index.php' as $arg.

routes.php

$route['edit/(:any)'] = 'edit/$1'

Accepts any param coming from the query string: yoursite.com/edit/param or yoursite.com/edit/2
It requires a method named edit.

If you are using $route=['default_controller'] = 'foo', as a container for all of your methods, change the route to $route['edit/(:any)'] = 'foo/edit/$1' or something like: $route['(:any)'] = 'foo/$1/$2' as the last line of your routes (note: this would work for yoursite.com/insert/param and yoursite.com/edit/param.

foo.php

    public function insert() { ... }

    public function edit($id=null) { ... }

    /* End of file foo.php */


.htaccess

    RewriteCond $1 !^(index\.php)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php?$1 [L]
Dawson
  • 7,567
  • 1
  • 26
  • 25
  • If I call only one view, it load's the function once, like is should. But I need that template and this is the way that it is done in the official CI tutorials. This is my edit function. Why my insert function is called once if I don't have slash at the end of the url and three times when i put a slash? I also have three views in my insert function. – Andrej May 10 '13 at 15:22
  • with the slash, it's probably looking for `/index.php`, or the method is trying to run with whatever argument it expects after the slash. See edit. – Dawson May 10 '13 at 16:15
  • I just looked through my base.php controller for my site. I'm using the HMVC pattern, and templates, so I'm not loading views like you are. – Dawson May 10 '13 at 16:16
  • OK, thanks. Any idea how to set my .htaccess or routes.php, so when I call /categories/edit/2, the edit function is not called three times? I don't want to set routing rules for every controller. I will, but only if that's the only solution. – Andrej May 11 '13 at 13:07
  • Added my setup to my answer. I have nothing specific to routes or methods inside the .htaccess file. – Dawson May 11 '13 at 16:03