Address of pages with codeigniter

Asked

Viewed 912 times

2

Hello! I’ll be very brief. I’m having trouble defining a correct href="". After clicking a link in the menu, it is directing 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 goes 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 am learning the codeigniter now and using it for a website I am developing. Would you have to modify in Routes.php or autoload.php? Could someone help me.

  • No need to add the route. How the url is post/reiki and the class is post, method reiki, CI will recognize. Anything you can add /posts/reiki. Maybe you are entering a URL that doesn’t really exist. You can also check the .htaccess (if your server is apache) https://gist.github.com/philipptempel/4226750

  • You can activate the url helper and use base_url('posts/reiki'). It may also be that the removal of index.php solve the case.

  • I made all the changes that commented, but nothing helped. Still pointing to page not found.

1 answer

0

To remove the need to always put index.php in the urls you need to create a . htaccess file at 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 that is in the application/config/ folder and add Halper

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

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

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

Now every time you need to use your website url just call the method base_url('posts/Reiki/'). In your example href would look something like:

href="<?php echo base_url('posts/reiki/')?>"
  • In case, my project is on a web server, would it be necessary to upload the file . htaccess or the server already has the necessary settings? In autoload.php and config.php I will make the changes and return already.

  • You have to upload the file. In general the servers accept by default .htaccess. The code I have put is the least you have to have to work. Its function is to redirect any request to your website to the index.php file

  • I try to upload the file . htaccess but it does not appear. what will be?

  • It doesn’t appear in the upando, nor in the root of my project

  • files starting with . are hidden files. See if your server is configured for you to view hidden files

  • Hello! I managed to resolve, the file . htaccess was hidden so I enabled to view in the configuration. After the changes you indicated solved my problem. Thank you!

Show 1 more comment

Browser other questions tagged

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