Codeigniter - uri段

时间:2012-01-03 17:44:30

标签: codeigniter controller

我需要创建一个这样的网址:www.example.com/index.php/scheda/name-surname-id.html

所以我创建了Scheda控制器,这是代码:

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

class Scheda extends CI_Controller{

    function __construct(){
        parent::__construct();
    }

    function index(){
        $name_scheda = $this->uri->segment(2);
        //i need only the id for the search into db
        $id = substr($name_scheda, strripos($name_scheda,'-')+1, strlen($name_scheda));

        echo "name:".$id;
    }
}

但是当我在地址栏中写下网址时,我会得到404 error ...有人可以帮我理解原因吗?

1 个答案:

答案 0 :(得分:5)

你的网址:

www.example.com/index.php/scheda/name-surname-id.html

应该是:

www.example.com/index.php/scheda/index/name-surname-id.html

index()是默认方法,但如果没有参数,则index段只能从URL中丢失,否则Codeigniter会认为您正在尝试调用方法name-surname-id.html()

您可以使用routes.php_remap()来清理网址并移除index细分。

// routes.php
$routes['scheda/(:any)'] = 'scheda/index/$1';

OR:

class Scheda extends CI_Controller{

    function _remap($method, $args) {
        $name_scheda = $method;
        $id = substr($name_scheda, strripos($name_scheda,'-')+1, strlen($name_scheda));
        echo "name:".$id;
    }
}