Codeigniter-friendly URL with emphasis on SEO

Asked

Viewed 277 times

0

Friends, I am developing a blog in CI. And thinking about the blog SEO, I would like the URL instead of being presented this way:

https://meusite.com.br/blog/postagem/1

I would like it to be shown with the title of the post.

https://meusite.com.br/blog/titulo-do-meu-post

My Route is like this:

$route['blog/(:num)'] = "blog/postagem/$1";

My controller is like this (don’t worry about the gambiarra, as soon as I solve this SEO issue I create the Models correctly):

class Blog extends CI_Controller {

    public function postagem($id){
        $this->db->where('id', $id);
        $data['blog'] = $this->db->get('blog')->result();
        $this->load->view('site/blog/postagem', $data);
    }
    public function home(){
        $data['blog'] = $this->db->get('blog')-> result();
        $this->load->view('site/blog/home' , $data);
    }

View posting:

<?php foreach ($blog as $post ) { ?>
    <h1><?php echo $post->titulo; ?>- <small><?php echo $post->categoria;?></small></h1>
    <p><?php echo $post->texto;?></p>
    <hr>
    <?php } ?>

View Home:

<?php foreach ($blog as $post ) { ?>
    <a href="<?php echo site_url('blog/postagem');?>/<?php echo $post->titulo; ?>"><?php echo $post->titulo?></a>
    <hr>
    <?php } ?>

I would like to know how the CI will actually identify that the title in the url corresponds to an ID on Route.. I can not understand, someone can help me?

2 answers

1


I believe that one of the requirements for you to find your answer is to understand how the Codeigniter interprets the Urls.

The representation would be that way:

dominio.com.br / classe / método / IDs e outros parâmetros

Therefore, when you access: https://meusite.com.br/blog/postagem/1, the class of Controller Blog is loaded and the method postagem is executed by receiving the parameter 1 via GET.

In the code is exactly what you already have:

<?php
    class Blog extends CI_Controller
    {

        public function postagem($id)
        {
            ...
        }
    }

Now, when you use routes, you are "masking" this. Internally everything works the same way, but the URI is treated differently, i.e., the Codeigniter carry out the remapping before execution.

If you want to transform your URL in something like this: https://meusite.com.br/blog/titulo-do-meu-post, you will need to configure the route as follows:

$route['blog/(.+)'] = 'blog/postagem/$1';

And then receive in the method postagem the parameter that will be the title of the post can follow the way @Phelipe suggested.

Sources:

https://www.codeigniter.com/user_guide/general/urls.html#Uri-Segments

https://www.codeigniter.com/user_guide/general/routing.html#Uri-routing

0

You can use the function url_title(). The function takes a string as input and creates a friendly URL string. You take the title of your post and go through this function and use it as part of the URL. You store this part of the url in your database and when searching your database for a news item, you do not ID, and yes to that part of the url.

I suggest reading an example that the Codeigniter documentation itself brings with it how to implement a news site. You can find this example here

  • Show ! This will help me.. I didn’t think there would be anything like that there! during this time I found in an American forum the following solution: <a href="<? php echo site_url(). 'blog/post/'. $post->id. '/'. implode('-',explode(' ',preg_replace('/[ w]+/','', $post->title))) ? > "><? php echo $post->title? ></a>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.