Welcome Guest, Not a member yet? Register   Sign In
Routing bad URLs
#1

(This post was last modified: 07-29-2016, 12:30 AM by pb.sajjad.)

Hi to all developers...

My question is about routing and redirect bad URLs.

Suppose I've a controller, User, with some methods, method_1, method_2, method_3. When I passed parameters to methods:

My_URL/user/method_1/integer
My_URL/user/method_2/chars
etc.

In each method, first I validate passed parameter and then do operations. 

My question is: when users type something like these:

My_URL/user/method_1/integer/integer/chars
My_URL/user/method_2/chars/integer/integer

or some bad URLs like these, is it a good approach to handle these problems with routing?

in fact, I write these routing (I know the first ones has more precedence):

PHP Code:
$route['user/method_1/(:num)'] = 'user/method_1/$1';
$route['user/method_1/(:any)'] = 'user/index'

I test it in localhost and every thing is good. When I enter bad URLs, like:

My_URL/user/method_1/integer/integer/chars

I redirect to user's index method.
Is it a good handling?
What is cos and pros?
Is it better I check in each method total segments and do handling there (URI library, [b]total_segments() methods)?[/b]

thanks...
Reply
#2

Honestly, it's going to depend on your environment, but in your example I would usually give the user a 404.

If it was an invalid parameter (not a case of too many parameters), I would usually redirect and display a validation message. In my environment, though, this is one of the few reasons I might send the user to an index page if the URL contained a method name. I usually have to assume that too many parameters is a bad link or someone trying to find some way to gain unauthorized access to data by manipulating the URL, and a 404 makes it easy to log and respond, if necessary, to either condition.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB