EllisLab text mark
Advanced Search
String as an URL
Posted: 06 September 2009 12:50 PM
Joined: 2009-08-07
98 posts


I’m trying to develop a basic blog/article system. I’m struggling to understand how to create the virtual pages and use the title of the blog as an URL.

For example http://localhost/article/this-is-my-first-blog

Posted: 06 September 2009 05:03 PM   [ # 1 ]   [ Rating: 0 ]
Joined: 2009-07-23
20 posts

Have you read about the url helper in the docs?



Famous Quotes

Posted: 06 September 2009 05:05 PM   [ # 2 ]   [ Rating: 0 ]
Joined: 2008-05-18
2398 posts

Have you looked at the handy url_title function in the URL helper?
Of course you’ll need to then query the database to make sure it’s unique

And then retreiving the page is a simple route (unless of course you want the controller and function in the URL) and a lookup

Edit: Darn it! Beaten again


I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse

Posted: 07 September 2009 03:26 PM   [ # 3 ]   [ Rating: 0 ]
Joined: 2009-08-07
98 posts

Yes, I have read the URL helper documentation.

What I don’t is once having queried the database how to retrieving the page. I can’t seem to find anything on it in the documentation.

Posted: 07 September 2009 07:05 PM   [ # 4 ]   [ Rating: 0 ]
Joined: 2008-07-16
411 posts

The easiest way (to support post title changes) is to also include the rows ID.


Which calls


My Blog, C2D, PHP Videos, Résumé, Super .htaccess, Extra hooks, and MicroMVC

Posted: 07 September 2009 08:14 PM   [ # 5 ]   [ Rating: 0 ]
Joined: 2007-11-28
2435 posts

Use a route to send all requests to a specific function, and then use that function to look up the URL and retrieve the corresponding post.

Posted: 08 September 2009 09:24 AM   [ # 6 ]   [ Rating: 0 ]
Joined: 2009-08-07
98 posts

Okay. So say I created a function called Article in my default controller named Welcome. I would then route using routes.php in the config folder and enter


How would I then get the information from the database to be passed into my view from the controller/model automatically depending on the ID selected?

Posted: 08 September 2009 09:28 AM   [ # 7 ]   [ Rating: 0 ]
Joined: 2009-03-10
1388 posts

Build a model. Add a function which gets the article base on $id. Return it to the controller. Then pass it along to the view.


Isset | Isset Public Code Repo | Simple Message Library | Session Profiler for CI2.0 | CI session issues in IE

Posted: 08 September 2009 10:07 AM   [ # 8 ]   [ Rating: 0 ]
Joined: 2008-03-13
515 posts

if you’re interested, the excellent Doctrine ORM has a built-in behaviour called ‘actas sluggable’
more info



Posted: 08 September 2009 10:55 AM   [ # 9 ]   [ Rating: 0 ]
Joined: 2007-06-11
2987 posts

These posts will only confuse the man!

georgerobbo: Doing this is very similar to working with ID’s. Normally you create a database record then use the ID number in the URL to fetch the content. Using a slug is exactly the same, but ofc first it needs to be made.


That will set it for your INSERT column, but is only one of many ways to achieve this. Pick your preferred syntax.

Then when you want to view an article, just do this:

class Blog extends Controller {
function view_article($slug)

Again a rather rough demo, that ofc should be in a model, but blog/view_article/some-title will now work fine. Shorten that with some routes and you are away!


Blog | Twitter | GitHub | BitBucket
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

Posted: 09 September 2009 03:07 PM   [ # 10 ]   [ Rating: 0 ]
Joined: 2009-08-07
98 posts

I’m really sorry but I’m still not quite understanding how to do it.

Say my database structure is as the following.

Primary_ID   Title   Description   Thumbnail   Content
1           example learning php   php.jpg       we’re going to learn how to use php

I then have the model stalker.php autoloaded, using the function get_article.

function get_article()

And then I have my controller named Article.

function index($slug)