2012-09-05 339 views
0

在我的CI應用我有幾個控制器來處理不同的用戶類型的功能:笨動態路由

  1. CA
  2. CD
  3. DV
  4. CSC
  5. CB

所以現在,當有人登錄時,他被他的角色重定向到(例如):localhost/CAlocalhost/CD等。

我需要重寫路線取決於他的角色都重定向:

$route['(:any)'] = 'CA/$1';使用時(CA不應該被硬編碼)

  • 規則也應該被刪除登錄控制器(通過篩選一些網址)

任何人都可以告訴我如何掛登錄後的規則?以及如何使用正則表達式來過濾某些應用規則的網址?

$route['^((?!auth/).)*$'] = '$1'; 

還有什麼其他方法可以實現這一目標? .htaccess不存在,因爲我需要一些數據邏輯來創建路由。

回答

0

這聽起來像你想要的東西,是落實remapping:標準的路由系統不是用來讓你改變基於用戶是否被登錄路由

但是,您可能(I」從來沒有見過這個工作)能夠把條件語句來檢查,看看是否在用戶登錄他們的作用是什麼(看一個會話cookie或類似的東西),然後裝入不同的路線標準routes.php文件文件選項。從來沒有嘗試過,但那可能會爲你工作。

+0

僅供參考我使用ion_auth,你可以給我更多的細節嗎? – Samson

+0

遺憾的是不是很熟悉ion_auth:在任何情況下,你可能想重新映射。 CI文檔涵蓋了這一點。 – Femi

+0

我不能使用_remap函數內部控制器,因爲當該功能被命中,所述控制器被理論上已經公知。我需要去「一級」 – Samson

1

由於this約從數據庫路由完美的事情教程中,我設法在用戶登錄後添加新的路由(在我的驗證控制器):

class Auth extends CI_Controller { 
function Auth() { 
    parent::__construct(); 
} 
function index() 
{ 
    if ($this->ion_auth->logged_in()) 
    { 
     $this->__checkRoles(); 
    } 
function __checkRoles() 
    {  
     $role=$this->ion_auth->get_group(); 
     $this->save_routes($role); 

     redirect(base_url().'index'); 

    } 
    public function save_routes($controller=null) 
    { 
      $output="<?php "; 
        //some uri's don't need routing 
      $output.="\$route['auth']='auth';";   
      $output.="\$route['auth/(:any)']='auth/$1';";      
      $output.="\$route['rest']='rest';"; 
      $output.="\$route['rest/(:any)']='rest/$1';"; 

        //for the rest route trough the "user-type" controller 
      $output.="\$route['(:any)']='".$controller."/$1';"; 

      $this->load->helper('file'); 
        //write to the cache file 
      write_file(APPPATH . "cache/routes.php", $output); 
    } 

我的routes.php文件是這樣的:

$route['404_override'] = ''; 
$route['default_controller'] = "auth"; 

include_once(APPPATH."cache/routes.php");