difficult URI Routing

I need to have these urls for my app:

/ajax/load/page/main - for controller frontend/index

/ajax/load/page/about - for controller frontent/get_page

/ajax/register for controller user/register
/ajax/login for controller user/login
/ajax/lost_p for controller user/lost_password

/ajax/user/200/ for controller user/get_page
/ajax/user/200/contacts for controller user/get_contacts
/ajax/user/200/comments for controller user/get_comments

/ajax/seller/300/ for controller company/get_page
/ajax/seller/300/contacts for controller company/get_contacts
/ajax/seller/300/comments for controller company/get_comments

200 and 300 are examples of ids

How to do these routing correctly? And how to receive params from controller?

URI Routing is as followS:

[folder] / [controller] / [function] / [param1] / [param2] / [paramN]

You - you can make a controller class called 'Ajax' which will contain the following functions:


Anything after the function call in the url is passed as a param.



Calls the 'foo' controller, finds within it the 'bar' function and passes 'bar' 3 params - 1, 2, and 3.

class Foo extends Controller {
    function bar ( $a , $b , $c ) {
        echo $a . $b . $c;
Will give you:

"123" in your browser.

I would suggest that you rearrange the urls so that they more naturally fit this pattern - it will save complex routing and make life easier all round.


May I use something like this?

$route['ajax/login'] = "frontend/login";
$route['ajax/logoff'] = "frontend/logoff";
$route['ajax/user/(:num)/about'] = "user/get_about_tab";
$route['ajax/user/(:num)/comments'] = "user/get_comments_tab";

I can't rearrange the urls because I am backend developer only =(

[quote author="SpaceCoder" date="1291326834"]May I use something like this?

$route['ajax/login'] = "frontend/login";
$route['ajax/logoff'] = "frontend/logoff";
$route['ajax/user/(:num)/about'] = "user/get_about_tab";
$route['ajax/user/(:num)/comments'] = "user/get_comments_tab";

I can't rearrange the urls because I am backend developer only =([/quote]

Yes, you can - if you've no control over the urls then you'll need to use routing. You'll need a 100% complete list of all possible urls so that you can 1) design an appropriate controller / function list and 2) define routes that will work exactly.

PITA though - these urls are a little backwards...

Routing gives you very fine control - you can, at worst, define a rule for every different URL. Not that you want to!

[eluser]Bart Mebane[/eluser]
If you use
$route['ajax/user/(:num)/about'] = 'user/get_about_tab/$1';
the id number will be passed as an argument to your controller function.

And how to specify different 404 error pages?

if requested
/ajax/smth/smth/*/smth - display mini 404 error for ajax.


/404/error/ - display full 404 page

Please, help!

[eluser]Bart Mebane[/eluser]
If you're using CI 2.0, you can specify a
$route['404_override'] = 'error_controller/not_found';
in the routes file. If you're using CI 1.7.2, it's not as easy. If you search the forums and the wiki for "custom 404", you'll find several ideas.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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