Welcome Guest, Not a member yet? Register   Sign In
difficult URI Routing
#1

[eluser]SpaceCoder[/eluser]
Hi!
I need to have these urls for my app:

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

/ajax/load/page/help,
/ajax/load/page/feedback,
/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?
#2

[eluser]Narkboy[/eluser]
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:

load
register_for_controller_user
login_for_controller_user
lost_p_for_controller_user
user
seller

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

So:

http://example/foo/bar/1/2/3

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

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

/B
#3

[eluser]SpaceCoder[/eluser]
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 =(
#4

[eluser]Narkboy[/eluser]
[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!
#5

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

[eluser]SpaceCoder[/eluser]
And how to specify different 404 error pages?

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

and

/404/error/ - display full 404 page
#7

[eluser]SpaceCoder[/eluser]
Please, help!
#8

[eluser]Bart Mebane[/eluser]
If you're using CI 2.0, you can specify a
Code:
$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.




Theme © iAndrew 2016 - Forum software by © MyBB