Welcome Guest, Not a member yet? Register   Sign In
[SOLVED!] Friendly URL and removing controller name?
#1

[eluser]fancms[/eluser]
I'm not even sure this is possible, but here goes:

I'd like to turn this:
http://site.com/page/category-title/page...title.html

into this:
http://site.com/category-title/page-id/page-title.html

Essentially, removing the controller (page) from the url. I have been playing with the routing features but can't quite figure out how to do this for just the pages controller. Any other routes - currently I have news/view/blah, among a couple of others - get directed to the page controller if I try doing a total wildcard like (.*)/(.*) (which I expected).

Any help would be appreciated; I tried looking at the documentation for the _remap function but there really isn't much for it, and I'm not sure that's the way I need to go anyway.

Big Grin
#2

[eluser]thurting[/eluser]
It probably isn't a good idea to structure things like that, but it can be done. The trick is to write a * rule and then overwrite it for the other controllers. However, if you have something like the following,

site.com/news (news controller)
site.com/category/id/title (page controller)

you have to be careful that you never have a page "category" that overlaps with any of your controller names. It is really a recipe for disaster.

Can I ask why you want to remove the controller name anyway? Users don't really care about URLs - most don't even look at them.
#3

[eluser]Michael Wales[/eluser]
Code:
$route[':any/:num/:any'] = "page/view/$2";

That route would translate the url (domain.com/site-news/36/site-launches-successfully.html) to the Page->view($id) method.

I would probably rewrite it to use RegEx, but I always have to go look that stuff up.
#4

[eluser]David Rothera[/eluser]
Would using that route means that if you had blah/blah/2 it wouldnt go to page/view?
#5

[eluser]fancms[/eluser]
Quote:you have to be careful that you never have a page “category” that overlaps with any of your controller names.
That would be easy to do with a simple callback check when they are adding page categories

Quote:Can I ask why you want to remove the controller name anyway? Users don’t really care about URLs - most don’t even look at them.
I'm targeting a specific niche with the program I'm writing and from all the research I did and from asking the webmasters what they liked, this is what they'd want to have for their content urls. The other controllers are fine as-is (ie, news/view/3/blah and links/view/3/blah).
Quote:
Code:
$route[':any/:num/:any'] = "page/view/$2";

That route would translate the url (domain.com/site-news/36/site-launches-successfully.html) to the Page->view($id) method.

I would probably rewrite it to use RegEx, but I always have to go look that stuff up.

Thanks, Michael! Using your example and a bit of experimenting I came up with this:

Code:
$route['([a-z0-9-_]+)/([0-9]+)/([a-z0-9-_]+)'] = 'page/view/$2';

This allows alphanumeric characters with dashes (-) and underscores (_). The other controllers still work as they should. Thanks again! :lol:
#6

[eluser]Michael Wales[/eluser]
The only thing you need to watch out for are other potential URLs that could meet the requirement for this rule. If you come across those - simply define a stricter rule prior to this catch-all rule. The following example makes little sense (SEO-wise), but proves the point:

domain.com/admin/4/edit would qualify for the catch-all rule, pointing it to Page->view(4) - obviously not what we want. We want it to go to Admin->edit_users() - here's how to correct:

Code:
$route['admin/:num/:any'] = 'admin/edit_users';
$route['([a-z0-9-_]+)/([0-9]+)/([a-z0-9-_]+)'] = 'page/view/$2';

Like I said, the example itself makes little sense.

The most common place I use these catch-all style routing is when I want to use a domain.com/username style of URL for member profiles. I define all of my known routes, then a catch-all route for anything else.
Code:
$route['signup'] = 'users/signup';
$route['login'] = 'users/login';
$route['admin/write'] = 'admin/new_post';
$route['admin/manage/([0-9]+)'] = "admin/edit_post/$3"; // Admin->edit_post($id = null)
$route['admin/manage'] = 'admin/edit_post';
$route['([a-z0-9-_]+)'] = "users/view/$1"; // Users->view($username = null)
#7

[eluser]fancms[/eluser]
Duly noted; thanks! Smile
#8

[eluser]Edemilson Lima[/eluser]
I am having a problem with a routing...

At my routes.php, I placed:

Code:
$route['([a-z]+)/([0-9]+)'] = "$1/details/index/$2";

If I try to access something like: http://www.site.com/products/details/index/3
It works fine.
But, if I do it by the route: http://www.site.com/products/3
It calls products/details/index, but does not send the id code:

Code:
A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Details::index()
Filename: products/details.php
Line Number: 11

"products" is a folder, "details" is the controller and "index" is its default function.

Any ideas?




Theme © iAndrew 2016 - Forum software by © MyBB