0
votes

It is my understanding that in a CMS like wordpress, all the pages are built dynamically by sending variables through the url and then simplifying the url to something like www.example.com/my-first-blog-post/. I'm assuming that it sends the post id and is not relying solely on the blog post's url name.

With codeigniter, I have my blog controller and my blog templates setup, however I'm not sure how I would implement something like wordpress. I would need to have a url like www.example.com/my-first-blog/12/ in order to pass the post's id. Is there a way to hide the 12 or do I instead search the blog post by it's url name?

2
It is not possible, since the controller must solve a data that you sent via GET, you must choose between the ID or name of the item (or 2 at a time) - csotelo

2 Answers

0
votes

You can use the url segment for post lookup. (these my-first-post like strings usually called slugs). You will have to pre-generate these and save them along with the posts.

You can use the routing configuration's $route['404_override'] to direct every otherwise non-routable path to a controller. Once you have that set up you will have to query with the parameter for blogposts and if it's found serve the post content, if not send out a regular 404. Something like this:

// application/config/routes.php
$route['404_override'] = 'blog/show_post'; // controller/action

// application/controllers/blog.php
class Blog extrends CI_Controller {
    public function show_post() {
        // get the first segment, the "first-post" from http://example.com/first-post
        $slug = $this->uri->segment(0); 
        $post = $this->posts->find_by_slug($slug); // imaginary posts model with db query
        if (!$post) { // if the post not found by slug
            show_404(); // return 404 as usual
        } else {
            $this->load->view('blog/show', array($post)); // post found, display it
        }
    }
}
0
votes

You can use the url segment for post lookup. (these my-first-post like strings usually called slugs). You will have to pre-generate these and save them along with the posts.

You can use the routing configuration's $route['404_override'] to direct every otherwise non-routable path to a controller. Once you have that set up you will have to query with the parameter for blogposts and if it's found serve the post content, if not send out a regular 404. Something like this:read this article http://www.obatkesehatanalami.com