• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remap or URI Routing or What?

Hey guys,

Am trying to do this:
/communities/(variable method)/(variable function)

What is the best way to do this? At the minute I am remapping everything in the controller so that if the method exists it goes to it but if it doesnt it goes to a standard method.

However, I find that it re routes to the method but then doesnt check to see if the function exists or not so i do it manually. However this url would not show an error page:

as it is rerouting and then carrying on as normal.

So basically i have this:
A community controller.
There are about 4-5 stanard methods, e.g. home, overview etc. and
For any community that exists, it is accessed using: /communities/(community title)

For each community there are various actions so e.g. view, members, stats, alerts etc e.g.
/communities/codeigniter/stats or /communities/the-uk-massive/join

What is the best way to accomplish this? As i said, i am currently remapping everything and testing to see if the method exists as a function or not. if it doesnt it goes to the "load community" method. Is this the best way?

So if I get it correctly:

/communities/overview will load the "overview" method
/communities/some_community will probably load a "show community" method
/communities/some_community/join will load a "join" method

You CAN use routes for this, but you'll have to specify all "real" methods:
// check the real methods to let them through
$route['communities/overview(.*)'] = 'communities/overview$1';
$route['communities/other_method(.*)'] = 'communities/other_method$1';

// check if a community + method was called. if so, rewrite it to load that method
// this does: communities/my-community/join/other/arguments -> communities/join/my-community/other/arguments
$route['communities/([^/]+)/([^/]+)(.*)'] = 'communities/$2/$1$3';
// a safer version of this would be to again specify all methods:
$route['communities/([^/]+)/some_method(.*)'] = 'communities/some_method/$1$2';

// finally, if a 'method' was called without anything after it, call the 'show community' method
$route['communities/([^/]+)'] = 'communities/show/$1';

Ok cheers. And would using routes be better than using the remap function?

At the minute, it tests if the community exists, if it does it then checks if the fucntion (i.e join/stats/members) is one of my prescribed and if it is then loads that method. I dont have my file here atm so cant post example of what im doing.

I guess what you suggested is better as if the function doesnt exist it will automatically display as page not found instead of me having to specify a list of functions?

I think both solutions are fine. A matter of taste.

You're right that if /communities/some-community/non-existing-method is called, it will give a 'page not found' error because it it rewritten to /communities/non-existing-method/some-community and that method is not found. But you have to decide if that is what you want. If you use _remap and you find that the community exists but the method doesn't, you can still just run the 'show community' method and prevent a 404 situation. That kind of thing is easier to do using _remap. If you use routes you try to be smart, but after the routes processing the method has to exist or you get a 404..

thanks again! What would I do if I wanted to remove the additional parameters from the uri if they are not needed? i.e. if i have this uri /communities/the-uk-massive/join/extra1/extra2

Can i automatically remove the extra 1 and 2 or would i have to test and then redirect back to the page?

Also, is there a preference for search engines whether i should show a 404 or just show the standard overview page if a method doesnt exist?

I would say just ignore those arguments. If you don't pay attention to them, no problem right? You could redirect, but I don't really see why. I think it makes your site more robust if it takes arguments even though they don't have any effect.

With regards to search engines: I think if there is a logical page to redirect to, do that. If there is not, show a 404. What I mean by this: for /communities/some-community/non-existing-method you could redirect to the /communities/some-community. That gives the user (or search engine) a page that is still related to what they were looking for. But if www.example.com/non-existing page is called, I would give a 404. If you don't, you would have to redirect everyhing to your homepage and that would be silly.

In my opinion the bottom line should always be: think of it from a user's perspective. If there is an error in the url, what is it that the user probably was looking for? Show that information. Thinking about users' needs will usually be okay for search engine rankings too.

Ok. Cheers for your help man!

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.