gabriel mellace
gabriel mellace

Reputation: 229

Pretty urls codeigniter

I have the following situation, I have some posts on my website which are access by id, lets say for example http://websiteurl.com/posts/see/1 the posts controller, has a see method which looks for the id on the database, lets say it find it and the posts title is "Most amazing photos" , how could i configure the Route.php class of codeigniter so that this posts its accessible from an url similar to:

http://websiteurl.com/posts/see/most-amazing-photos-1.html

this posts are created dynamically and they are a lot, so setting one by one manually is not possible.

Thanks!

Upvotes: 0

Views: 106

Answers (2)

tomexsans
tomexsans

Reputation: 4527

The url parameter you would like to achieve is called a slug

like wordpress does, you can query the POST via its id or its slug.

One way to implement this is every time a post is created, you save the slug to the db.the slug can be the title of the post or anything readable for SEO. So that everytime a user visits the site, your controller method may prcess the parameter if it is a slug or an id and continue the query using the given parameters.

then you can set up the routes like:

$route['posts/see/(:any)'] = "posts/see/$1";

Upvotes: 0

Dirgh
Dirgh

Reputation: 507

What you can do is use regex. Assuming that you will be creating different url tag for each photo you can have something like this in you route.php

$route['posts/see/(:any)'] = "posts/see/$1";

However it is necessary that you have unique url-tag for each photo otherwise it will be ambiguous to fetch photo from the url-tag.

Have it a go.

Upvotes: 1

Related Questions