2014-09-28 90 views

回答

4

您必須從codeigniter官方文檔中讀取page。它涵蓋了所有與路由URL相關的事情。

application/config/routes.php 

這可能是這樣的:所有路由必須通過文件來配置

$route['user/(:any)'] = "user/user_detail/$1"; 
+0

$路線[ '用戶/(:任何)'] = 「用戶/ user_detail/$ 1」; $ route ['(:any)'] =「user/user_detail/$ 1」; – 2014-09-28 11:05:47

3

這可以通過重寫CI_Controller類來實現,但不改變原來的核心文件,就像我說的覆蓋控制器,並把你的邏輯在裏面。

幫助:https://ellislab.com/codeigniter/user-guide/general/core_classes.html

how to create Codeigniter route that doesn't override the other controller routes?

也許是一個更容易的解決辦法是使用Apache的mod_rewrite在.htaccess的幫助下將其路由

下面是關於如何實現它的詳細解釋:http://www.web-and-development.com/codeigniter-remove-index-php-minimize-url/

3

Hatem的回答(使用路由配置)更容易和更清潔,但指向的用法_remap()功能在某些情況下可能會有所幫助:

CI_Controller的內部,_remap()函數將在每次調用控制器時執行,以決定使用哪種方法。在那裏你可以檢查方法是否存在,或者使用一些定義的方法。你的情況:

application/controllers/User.php

class User extends CI_Controller { 
    public function _remap($method, $params = array()) 
    { 
     if (method_exists(__CLASS__, $method)) { 
      $this->$method($params); 
     } else { 
      array_unshift($params, $method); 
      $this->user_detail($params); 
     } 
    } 

    public function user_detail($params) { 
     $username = $params[0]; 
     echo 'username: ' . $username; 
    } 

    public function another_func() { 
     echo "another function body!"; 
    } 
} 

這將導致:

http://www.example.com/user/user_detail/john =>'用戶名:約翰 http://www.example.com/user/mike ........... = >'username:mike' http://www.example.com/user/another_func ... =>'另一個功能主體!'

但它不會一起工作:http://www.example.com/mike,因爲控制器 - 即使它是默認控制器 - 在所有不叫,在這種情況下,CI默認行爲是查找一個名爲mike控制器如果沒有找到它會拋出404錯誤。

更多:

Codeigniter userguide 3: Controllers: Remapping Method Calls

Redirect to default method if CodeIgniter method doesn't exists.

相關問題