2013-07-28 66 views
0

我正在使用代碼點亮器來編寫網站。這是我當前的.htaccess文件。代碼點亮器.htaccess

RewriteEngine on 
RewriteCond $1 !^(index\.php|css|js|images|robots\.txt) 
RewriteRule ^(.*)$ /ci/index.php/$1 [L] 

我有一個用戶控制器,除其他外,從URL像這樣

mysite.com/user/getProfile/$username

我會訪問時顯示輪廓喜歡做的是擺脫getProfile,以便mysitecom/user/$用戶名將調用get配置文件功能。儘管仍然保持上面的代碼。如果我必須犧牲在控制器中能夠擁有其他功能,那麼就可以。

任何建議表示讚賞!

+3

在routes.php文件文件中添加以下內容: $路線['用戶/(:任何)'] ='user/getProfile/$ 1'; – Ula

+0

我同意Ula,routes.php應該被用來處理CI中的路由請求,而不是.htaccess文件。 – Ohgodwhy

回答

0

在你的「用戶」控制器中,不要在getProfile()函數中傳遞$ username。

你會做的是...你在默認函數中傳遞$用戶名。

舉個例子,你有這個索引函數,那麼默認的登陸頁面是index()。

,並在你的路由器,你有這樣的配置

$route['default_controller'] = "user/index"; 
$route['(:any)'] = 'user/index/$1'; 

而在你的控制器類,這應該是這樣的......

class User extends CI_Controller { 

    public function index($page = 'user') 
    { 
     if ($this->uri->segment(2) != '') { 
      $data['Profile'] = $this->_getProfile($this->uri->segment(2)); 
     } 

     $this->load->view($page, $data); 
    } 

    private _getProfile($usernanme) 
    { 
     // This will return the profile of the user 
    } 

} 

希望這是很清楚... :)

0

以下是CI Url重寫的示例

首先.htaccess擺脫索引。 PHP

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /index.php [L] 
</IfModule> 

然後在config.php編輯

$config['index_page'] = ''; 

然後控制器和routes.php文件只是簡單的例子,在config文件夾

$route['default_controller'] = "main"; 
$route['404_override'] = ''; 
$route["contact"] = 'main/contact'; 
$route["(.*)"] = 'main/tekstovi/$1'; 

而且例如控制器

class Main extends CI_Controller { 


    public function index() 
    { 
     $this->home(); 
    } 

    public function home() 
    { 


     $this->load->view("view_home", $data); 

    } 


    public function tekstovi() 
     { 

     $data['results'] = $this->get_db->('TABELNAME',$this->uri->segment(1)); 
     $this->load->view("view_tekstovi", $data); 

    } 
    public function contact() 
     { 

     $this->load->view("view_contact", $data); 

    } 


} 

您可以添加.html在URL的末尾只需要編輯config.php文件

$config['url_suffix'] = '.html'; 

這僅僅是簡單的例子,嘗試添加自己的:)