2012-01-03 46 views
0

我需要創建這樣一個URL:www.example.com/index.php/scheda/name-surname-id.html笨 - URI段

所以我創建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 ...有人可以幫助我瞭解爲什麼?

回答

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中缺少如果沒有參數,否則笨會認爲你正試圖調用方法name-surname-id.html()

您可以使用routes.php_remap()清理URL並刪除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; 
    } 
} 
+0

那正是我需要的,非常感謝你的人! – 2012-01-03 17:56:42