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

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

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

Have you read about the url helper in the docs?

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

 Signature 

Famous Quotes

 
Posted: 06 September 2009 05:05 PM   [ # 2 ]   [ Rating: 0 ]
Avatar
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

 Signature 

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 ]
Avatar
Joined: 2008-07-16
411 posts

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

/blog/read/23/the_post_slug 

Which calls

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

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

 
Posted: 07 September 2009 08:14 PM   [ # 5 ]   [ Rating: 0 ]
Avatar
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

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

 
Posted: 08 September 2009 09:28 AM   [ # 7 ]   [ Rating: 0 ]
Avatar
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.

 Signature 

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 ]
Avatar
Joined: 2008-03-13
515 posts

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

 Signature 

:wq

 
Posted: 08 September 2009 10:55 AM   [ # 9 ]   [ Rating: 0 ]
Avatar
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.

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

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

 Signature 

————————
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()
    
{
        $this
->db->set('slug'url_title($this->input->post('Title')));
        
$this->db->set('title'$this->input->post('Title'));
        
$this->db->insert('articlebase');
    

And then I have my controller named Article.

function index($slug)
    
{
        $data[
'title''Article';
        
        
$this->db->where('stalker'$slug);
        
$this->db->get();
        
        
$this->load->view('meta'$data);
        
$this->load->view('header');
        
$this->load->view('content'$data);
        
$this->load->view('index'$data);
        
$this->load->view('footer');