CodeIgniter Forums
String as an URL - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: String as an URL (/thread-22352.html)

Pages: 1 2


String as an URL - El Forum - 09-06-2009

[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


String as an URL - El Forum - 09-06-2009

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

http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html


String as an URL - El Forum - 09-06-2009

[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


String as an URL - El Forum - 09-07-2009

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


String as an URL - El Forum - 09-07-2009

[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');



String as an URL - El Forum - 09-07-2009

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


String as an URL - El Forum - 09-08-2009

[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?


String as an URL - El Forum - 09-08-2009

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


String as an URL - El Forum - 09-08-2009

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


String as an URL - El Forum - 09-08-2009

[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!