Codeigniter,路由和404错误

时间:2013-11-26 16:15:17

标签: php codeigniter codeigniter-routing

这是我的routes.php

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

这是我的控制器,基地:

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);
    }
}

问题在于,网址:

/operativa/nuevo_cliente

将触发404而不是echo

任何想法我都错过了吗?

PD:预期的指数和家庭工作

3 个答案:

答案 0 :(得分:1)

问题似乎是你的网址与第一条路线匹配,所以它永远不会到达你希望它去的功能。

您应该将路线从更具体的更改为更一般的路线,如下所示:

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

答案 1 :(得分:0)

首先,echo 'El tipo es: '.$tipo.'<br>';必须在switch语句之后。 为什么你在控制器中回应这个? 并执行if语句而不是切换。

答案 2 :(得分:0)

EDIT !!!你不需要switch语句......试试这个,看看会发生什么(仅用于测试):

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);
}

你也可以改变这样的路线顺序:

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