Welcome Guest, Not a member yet? Register   Sign In
Simple routing question. Please help.
#1

[eluser]technocrat[/eluser]
Just started with CI, must admit its truly a delight to use! A simple question regarding routing, the (otherwise hepful) documentation could not help me here!

Code:
Can
url : example.com/class_name/function_name/username/slug-to-entry

Be made to:
url : example.com/username/slug-to-entry ?


many thanks!
#2

[eluser]darrentaytay[/eluser]
Yes, in application/config/routes.php, you can define routes.

For example:

Code:
$route['(:any)/(:any)'] = "controller/method/$1/$2";
#3

[eluser]technocrat[/eluser]
Thank you for the reply darrentaytay! how would i prevent it from clashing with the existing functions?

For Example, how do i ensure the following works?

example.com/username/edit/slug-to-entry
example.com/username/slug-to-entry

Sorry if this is a very trivial and simple question, but would be greatful if you could help out further!
Many thanks!
#4

[eluser]darrentaytay[/eluser]
Normally it's wise to use some sort of prefix for the type of URL to avoid clashing, so if you are editing entries:

Code:
$route['entry/edit/(:any)/(:any)'] = "entry_controller/edit_method/$1/$2";

As expected, if you just use the code from my previous example, any call to a URL which has 2 parameters it will be directed to that method unless you add more routes.

I'd recommend, if you haven't done so already, reading the User Guide:

http://ellislab.com/codeigniter/user-gui...uting.html
#5

[eluser]technocrat[/eluser]
I have managed to get routing for the two variables happening. So there is no way of having:

Code:
example.com/username/entry-slug
without clashing with other functions?

Again, thank you for your time. Greatly appreciate it.
#6

[eluser]darrentaytay[/eluser]
In my experience, there is no simple way to achieve this ( someone please correct me if I'm wrong )

I know it's a pretty looking URL to have it that way, but perhaps a little simple segment to identify the URL would work.

Code:
example.com/e/username/entry-slug

Where e would mean it was an "entry" or something.

Then you could route:

Code:
$route['e/(:any)/(:any)'] = "entry/$1/$2";
#7

[eluser]technocrat[/eluser]
I tried functioning that. but then again, i dint want the "e" portion in the URL. I tried the following:

Code:
$route['(:any)/(:any)'] = "entry/view/$1/$2";
$route['function/(:any)/(:any)'] = '$1/$2';


$route['function/auth/login'] = "auth/login";
$route['function/auth/logout'] = "auth/logout";
$route['function/entry/delete'] = "entry/delete";
$route['function/entry/add'] = "entry/add";

prefixed all functions with a /function instead, but if have to add a route for every function. If there exists a better way to function this, kindly let me know; it would be of great help!
#8

[eluser]darrentaytay[/eluser]
Try moving:

Code:
$route['(:any)/(:any)'] = "entry/view/$1/$2";

Below the other routes - the higher the route in the file, the higher the priority.
#9

[eluser]ηυмвєяσηє[/eluser]
this select only 2 segments.. then you wont need to define $route['function/auth/login'], $route['function/auth/logout'] etc.

Code:
$route['([^/]+)/([^/]+)'] = "entry/view/$1/$2";
#10

[eluser]technocrat[/eluser]
thanks darrentaytay and ηυмвєяσηє! taking both your advice, i have rewritten my routes as follows:

Code:
$route['do/([^/]+)/([^/]+)'] = "$1/$2";
$route['([^/]+)/([^/]+)'] = "entry/view/$1/$2";

hope this will help others searching for the same solution.

on a sidenote, how would i achieve pagination?
Code:
example.com/username/entry_slug/page_num

Thanks a million for all your help, they have benefitted me greatly!
Technocrat




Theme © iAndrew 2016 - Forum software by © MyBB