• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String as an URL


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

Have you read about the url helper in the docs?


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

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.

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


Which calls

blog->read(23, 'the_post_slug');

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.

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

$route['welcome/article/(:num)'] = "welcome/article/article_lookup_by_id/$1";

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?

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.

[eluser]GSV Sleeper Service[/eluser]
if you're interested, the excellent Doctrine ORM has a built-in behaviour called 'actas sluggable'
more info

[eluser]Phil Sturgeon[/eluser]
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.

$this->db->set('slug', url_title($this->input->post('title')));
$this->db->set('title', $this->input->post('title'));

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)
    $this->db->where('slug', $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!

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.