Addressing pages with codeigniter

1

Hello! I'll be very brief. I'm having trouble setting a correct href="". After clicking on a link in the menu, it is pointing to the HOSTGATOR page '404 - page not found' here's the controller code:

class Posts extends CI_Controller {

    //Página de listar posts
public function index()
{
        // Carrega o model posts
        $this->load->model('posts_model', 'posts');

        // Criamos a array dados para armazenar os posts 
        // Executamos a função do posts_model getPosts
        $data['posts'] = $this->posts->getPosts();

        // Carregamos a view listarprodutos e passamos como parametros a array posts que guarda todos os posts
        // da db posts
    $this->load->view('listarposts', $data);
}

// Página de listar reiki
public function reiki()
{

        // Carrega a model posts
        $this->load->model('posts_model', 'posts');

        // Carrega a view
        $this->load->view('listarreiki');
}

}

Here's the view with href with the link:

  <ul class="list-unstyled">
                <li><a href="posts/reiki">Reiki</a></li>        
      </ul><span class="heading">Email</span>

I'm now learning codeigniter and using it for a website I'm developing. Would it have to be modified in routes.php or autoload.php? Someone could help me.

    
asked by anonymous 07.01.2018 / 18:54

1 answer

0

To remove the need to always place index.php in urls you need to create a .htaccess file in the root of your project with the following code:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

It is also advisable to put the full path in the urls. To do this codeigniter provides a helper called url to assist in this process. To use this helper go to the autoload.php file in the application / config / folder and add the halper

$autoload['helper'] = array('url');

Now in the config.php file that is in the application / config / folder you place your url base. It will look something like:

$config['base_url'] = 'http://localhost/meuprojeto/';

Now every time you need to use the url of your site just call the base_url ('posts / reiki /') . In your example href would look something like this:

href="<?php echo base_url('posts/reiki/')?>"
    
09.01.2018 / 04:10