views:

611

answers:

1

I am brand new to codeIgniter.

I am very particular about urls on the sites that I develop. Is it possible to create these sorts of urls? Generally sites I develop have an integrated admin interface as well with new, edit or delete added onto the end of the url following a slash.

Here are some hypothetical examples (one with an admin url):

top level pages (no trailing slash)

site.com/about
site.com/contact
site.com/contact/edit

section index lists (lists have trailing slash)

site.com/blog/
site.com/products/
site.com/products/edit

section pages (lists have trailing slash)

site.com/blog/first-post
site.com/products/best-product
site.com/products/new
site.com/products/best-product/delete

section categories

site.com/blog/code-questions/
site.com/products/red-products/
site.com/products/red-products/delete

the first problem I see is sending a url with trailing slash to a different controller then without one. Since you can't add them in the routing file. For instance with top level pages how would I know to call the Pages controller? how do I tell them apart from section index lists? I can't add trailing slashes in routes.php!

site.com/about
site.com/blog/

same with the section pages vs categories.

generally I have done this in the past with an .htaccess file.

some examples of how I structure htaccess files for urls in the past in my own applications

RewriteRule ^new$ index.php?static&new
RewriteRule ^edit$ index.php?edit
RewriteRule ^([a-z0-9\-]+)$ index.php?static&post=$1
RewriteRule ^([a-z0-9\-]+)/edit$ index.php?static&edit=$1

RewriteRule ^([a-z0-9\-]+)/$ index.php?section=$1
RewriteRule ^([a-z0-9\-]+)/new$ index.php?section=$1&new
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)$ index.php?section=$1&post=$2
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/$ index.php?section=$1&category=$2

Is there anyway to do this with codeIgniter? Should I just slather rewrite rules on top of the controller generated urls? Is it possible to do this with the routing.php file? If codeIgniter doesn't do this, could you suggest a framework that can?

Also How do I handle using hyphens in the url when tied to the controller class name?

+1  A: 

CodeIgniter has a pretty nice way of handling URI routing (http://codeigniter.com/user%5Fguide/general/routing.html).

By default if you use CodeIgniter's own .htaccess (http://codeigniter.com/user%5Fguide/general/urls.html), all requests made into pretty segments.

You could easily use CodeIgniter's routing.php file to generate these URL's you're after. Please refer to their very easy to follow documentation at http://codeigniter.com/user%5Fguide/.

Jedi
$routing['blog/view/:num'] = 'section/blog/view/$1';$routing['products/:num'] = 'section/products/view/$1';
Jedi
If I have understood right, you want just a trailing slash to denote the action to be different? I'm not sure if this is a very practical way to do what you're trying to do. I would rather use something like your "/new" segment.
Jedi
That is correct. so that site.com/([a-z0-9\-]+) would load the TopPages controller and site.com/([a-z0-9\-]+)/ would load the blog controller and so on.
Okay. I don't see why you would want to do that. You could do: $routing[':any'] = 'TopPages/$1'; $routing['blog/:any'] = 'blog/$1'; ?
Jedi