Search code examples
phpcodeignitercodeigniter-routing

Codeigniter, routes and 404 error


this are my routes.php

$route['(:any)'] = 'base/index/$1';
$route['home'] = 'base/home';
$route['operativa/(:any)'] = 'base/operativa/$1';
$route['default_controller'] = "base";
$route['404_override'] = '';

This is my controller, base:

class Base extends MY_Controller {

    function __construct() {
        parent::__construct();      
        $this->load->model('filesmodel', 'files');
        $this->load->model('cestamodel', 'cesta');
    }
    public function index ($pagina = 'login')  {
        $data = array(
            'page' => $pagina,
            'logado' => false
        );
        $this->load->view('modules/top', $data);
        $this->load->view('pages/'.$pagina, $data);
        $this->load->view('modules/bottom', $data);
    }
    public function home (){
        $pagina = 'home';
        $data = array(
            'page' => $pagina,
            'logado' => true,
            'notifications' => 3
        );
        $this->load->view('modules/top', $data);
        $this->load->view('pages/'.$pagina, $data);
        $this->load->view('modules/bottom', $data);
    }
    public function operativa($tipo){
        echo 'El tipo es: '.$tipo.'<br>';
        switch ($tipo) {
            case 'nuevo_cliente':
                $pagina = $tipo;
                break;
            case 'contacto':
                $pagina = $tipo;
                break;
            case 'mensajes':
                $pagina = $tipo;
                break;
        }
        $data = array(
            'page' => $pagina,
            'logado' => true,
            'notifications' => 2
        );
        $this->load->view('modules/top', $data);
        $this->load->view('pages/operativa/'.$pagina, $data);
        $this->load->view('modules/bottom', $data);
    }
}

Problem is that, url:

/operativa/nuevo_cliente

Will fire 404 instead of the echo

Any idea wha am I missing?

PD: index and home work as expected


Solution

  • The problem seems to be that your URL is matching the first route, so it never gets to the function you want it to go.

    You should rewrite your routes from more specific to more general, like so:

    $route['home'] = 'base/home';
    $route['operativa/(:any)'] = 'base/operativa/$1';
    $route['(:any)'] = 'base/index/$1';
    $route['default_controller'] = "base";
    $route['404_override'] = '';