Toni Michel Caubet
Toni Michel Caubet

Reputation: 20173

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

Upvotes: 0

Views: 410

Answers (3)

Chiribuc
Chiribuc

Reputation: 88

EDIT!!! You don't need the switch statment...Try this and see what happens (just for testing):

public function operativa($pagina){

 if ( ! file_exists('application/views/pages/operativa/'.$pagina.'.php'))
{
    // Whoops, we don't have a page for that!
    show_404();
}
    $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);
}

You could also change the order of the routes like this :

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

Upvotes: 0

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'] = '';

Upvotes: 1

Chiribuc
Chiribuc

Reputation: 88

First of all the echo 'El tipo es: '.$tipo.'<br>'; must be after the switch statment. And why do you echo this in your controller ? And do an if statment instead of switch.

Upvotes: 0

Related Questions