Welcome Guest, Not a member yet? Register   Sign In
Correct way to re-direct in controller back to "index"
#1
Question 
(This post was last modified: 02-04-2024, 11:37 PM by joho.)

What is the correct way to re-direct the user back to "index"?

In my routes, I'm using $routes->match() to match URIs according to CRUD, so for "delete", there's:
Code:
$routes->match( ['get', 'post'], 'delete/(:segment)', [MyController::class, 'delete'] );

And then in "delete", in the Controller, I check for a valid parameter. If I don't get a valid parameter, I want to issue a re-direct to "/".
I've tried returing the "index" view, which is not good since it'll keep the URI at domain.com/delete.

I've tried setting up an additional route, as a catch-all for "delete" like so:
Code:
$routes->match( ['get', 'post'], 'delete/', [MyController::class, 'index'] );

But that will yield the same result (which is expected).

I've tried using redirect()->route like so:
Code:
return( redirect()->route( '' ) );

But that'll re-direct the browser to "https://domain.com/index.php/", which is ugly :-)

So, what I'm using now is simply this:
Code:
return( $this->response->redirect( base_url() ));

Is that the correct way of doing it?

-joho
Reply
#2

You should not delete something with GET requests. GET should be safe (does not change any resources).

See https://codeigniter.com/user_guide/outgo...a-uri-path
and https://codeigniter.com/user_guide/insta...-site-uris
Reply
#3

Well, I'm not actually deleting anything on GET. That will simply display a confirmation prompt, which will do a POST, which will delete.

-joho
Reply




Theme © iAndrew 2016 - Forum software by © MyBB