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

#1
[eluser]georgerobbo[/eluser]
Hello,

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

#2
[eluser]darrenfauth[/eluser]
Have you read about the url helper in the docs?

http://ellislab.com/codeigniter/user-gui...elper.html

#3
[eluser]Dam1an[/eluser]
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

#4
[eluser]georgerobbo[/eluser]
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.

#5
[eluser]Xeoncross[/eluser]
The easiest way (to support post title changes) is to also include the rows ID.

Code:
/blog/read/23/the_post_slug

Which calls

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

#6
[eluser]Aken[/eluser]
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.

#7
[eluser]georgerobbo[/eluser]
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

Code:
$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?

#8
[eluser]n0xie[/eluser]
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.

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

#10
[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.

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

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:

Code:
<?php
class Blog extends Controller {
  
  function view_article($slug)
  {
    $this->db->where('slug', $slug);
    $this->db->get();
  }
}
?>

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.