Why do Codeigniter 3.1 routes not work?

Asked

Viewed 248 times

1

I’m a beginner in PHP, recently decided to learn and started reading the book "Codeigniter - Productivity When Creating Web Applications in PHP". In an attempt to follow the creation of an institutional website, by implementing an element With a ul(unorder list), each li contains an anchor, I have my anchors with the attribute

<a href="<?=base_url('empresa')?>" >A Empresa</a>

that loads base_url and concatenates the company route' - The question is whether it’s working

[project directories]

exemplos-livro-ci - site-institucional - application -
                                                   - cache
                                                   - config 
                                                   - controllers
                                                               - Institucional.php
                                                   - helpers 
                                                   - hooks
                                                   - language
                                                   - libraries
                                                   - logs
                                                   - models
                                                   - thirdparty
                                                   - views
                                      - assets(contém meus ficheiros css, js, img)
                                      - composer.json
                                      - .gitignore
                                      - .editorconfig
                                      - índex.php
                                      - license.txt
                                      - **web.config**

[Code] Controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Institucional extends CI_Controller {
     public function index() {
        $data['title'] = "LCI | Home"; 
        $data['description'] = "Exercício de exemplo do capítulo 5 do livro CodeIgniter";
        $this->load->view('home',$data);
     }
     public function Empresa() {
        $data['title'] = "LCI | A Empresa"; 
        $data['description'] = "Informações sobre a empresa";
        $this->load->view('commons/header',$data); 
        $this->load->view('empresa'); 
        $this->load->view('commons/footer');
     }
     public function Servicos() {
        $data['title'] = "LCI | Serviços"; 
        $data['description'] = "Informações sobre os serviços prestados";
        $this->load->view('commons/header',$data); 
        $this->load->view('servicos');
        $this->load->view('commons/footer');
     }
}
?>

[Code] View

<nav>
<?php if($this->router->fetch_class() == 'Institucional' && $this->router->fetch_method() == 'index'){?>
<ul class="nav masthead-nav">
<?php } else {?>
<ul class="nav navbar-nav">
<?php } ?>
<li class="<?=($this->router->fetch_class() == 'Institucional' && $this->router->fetch_method() == 'index') ? 'active' : null; ?>"><a href="<?=base_url()?>" >Home</a></li> 
<li class="<?=($this->router->fetch_class() == 'Institucional' && $this->router->fetch_method() == 'Empresa') ? 'active' : null; ?>" ><a href="<?=base_url('empresa')?>" >A Empresa</a></li> 
<li class="<?=($this->router->fetch_class() == 'Institucional' && $this->router->fetch_method() == 'Servicos') ? 'active' : null; ?> "><a href="<?=base_url('servicos')?>" >Serviços</a></li> 
<li><a href="<?=base_url('trabalhe-conosco')?>">Trabalhe Conosco</a></li> 
<li><a href="<?=base_url('fale-conosco')?>">Fale Conosco</a></li> 
</ul>
</nav>

[Estates] application config config.php

$config['base_url'] = 'http://localhost/exemplos-livro-ci/site-institucional';

[Estates] application config Routes.php

$route['default_controller'] = 'Institucional';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['empresa'] = "application/controllers/Institucional/Empresa";
$route['servicos'] = "application/controllers/Institucional/Servicos";

[Debbug attempts] - When I access my index with the url http://localhost/examples-book-ci/site-institutional the browser displays my HOME PAGE correctly. When I click on one of the anchors( element), I have the error 404 - Requested URL not found on this server. When I search the complete path without recourse to my route(http://http://localhost/examples-book-ci/site-institutional/application/controllers/Institutional/services) have the error 403 - You are not allowed to access the requested object

[Looking for information] - It would be interesting to improve debbuging, know if the routes are being assigned. How do I do that? Maybe through the controller pass $data to the view and display?.

Technologies used. xampp v3.2.2. Codeigniter v3.1.10 php v3.6.40

1 answer

-1

At the root of the project, create a .htaccess with the lines below:

Options FollowSymLinks
<IfModule mod_rewrite.c>    
    RewriteEngine On 

    RewriteCond $1 !^(index\.php|assets|images|css|js|robots\.txt|favicon\.ico)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

And on the routes the settings should be without (application/controllers/), as in the example below:

$route['empresa'] = "Institucional/Empresa";
$route['servicos'] = "Institucional/Servicos";

Browser other questions tagged

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