views:

93

answers:

3

Hi

I'm kind of new with CodeIgniter and I'm still learning (a lot).

So I have a view and when I submit a form I 'call' the controller by surfing to the right URL dynamically e.g. site/delete

    class Site extends Controller {

    function index(){$this->load->view('...')}

    function delete() {
        $this->site_model->delete_row();
        $this->index();
    }

    }

Now when that action is done (deleted the row) I'm calling $this->index(); to redirect to my initial page (which is good) but my url stays: site/delete . I want my URL to be ../site/index (or without the /index)

Any help would be appreciated :-) .

+2  A: 

So far I found something to solve this:

instead of:

$this->index();

I'm using:

redirect('site');

Does anyone know this is a good practice?

Ayrton
Redirect fine for me, but it causes the server to send additional response headers, so it requires a little bit more overhead.
caseyamcl
"additional response headers" isn't quite true, it triggers a whole new request (and response). But yes, there is a slight additional overhead. This is still the right solution though. Often in cases like this you'll also stick something into CI's flashdata which the index method reads and forwards on to the user (so you can tell them the action completed successfully).
Sid
+1  A: 

Redirect is what you should use.

In the user guide: http://codeigniter.com/user_guide/helpers/url_helper.html

they use it after checking if a user is logged in. Depending on if they are or not, they redirect to a different place.

Also, note that any code after the redirect won't run. Make sure and redirect after you've done everything you need to.

Matthew
A: 

My preferred method is to have actions like that handled by the same method that will be seen by the user afterwards.

What if you go to /site/delete afterwards, as a user? It will either have to detect and throw a error (show a message) or redirect to an appropriate page. /site/delete has no meaning.

For example, if a user would normally see an overview after deleting, then my form will be posted to /site/index; with index quickly checking for the condition and calling _delete() in the same controller, before doing its normal work.

That way, if the user refreshes the page, or presses 'back', things should look consistent to them.

Another example would be that /settings/edit would post to itself - this means that it can act on the post and show any output (e.g. validation errors). It means there's no /settings/do_edit location on my site, and also means that the user can go back to /settings/edit safely, and see a form for editing their settings.

I suppose this is a subjective take on a perhaps objective question, and I would encourage feedback on my view, but it's my way of avoiding the problem you have asked about.

Kurucu
The potential problem you can run into here is if re-posting (by refreshing) will do something undesirable (eg. add another product to the basket). Either use redirects in occasions like this or post, and handle, nonces (which would also protect against CSRF attacks).
Sid
Hi Sid. Thanks for your comment. I guess a nonce is a use-once token that I define when I create the form? Then chuck it away once used or after a time limit expires? This could lead to a whole new question on SA for me!
Kurucu