Question

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 :-) .

Was it helpful?

Solution

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?

OTHER TIPS

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.

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.

$this->index();

Call of function in a function simply execute the functionality within that function. And url never changed.

for changing the url you should use.

redirect ( base_url().'site');

but you should load url helper in constructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top