Search code examples
codeigniterroutessystemblogsposts

Code Igniter: Creating a blog cms: display a blog post that is given in the URL


URL: www.blog.com/posts/category/post-title

How do I create a controller, that accepts the two parts of the URL "category" and "post-title" and echoes out the post?

Thank you a lot.

Timo


Solution

  • In CodeIgniter the URL is mapped as domain/Controller/Method/params, so following URL

    www.blog.com/posts/category/post-title
    

    will be mapped as posts is the controller, category as method name and rest are parameters. So, You need to create a controller like this:

    // posts.php
    class Posts extends CI_Controller {
    
        public function show($category, $title)
        {
            // ...
        }
    }
    

    Then the URL could be www.blog.com/posts/show/some-category/post-title and you can retrieve the some-category and post-title as parameters in you show method. For more information, check Controllers on CI User Guide.