2011-12-16 41 views
0

請忍受我,因爲我是一個cakephp noob。我有這個應用程序,應該去www.name.com/complexes/somecomplex/unitnumber。我可以正確地將它提前到www.name.com/complexes/somecomplex,但我不知道如何獲取單元號的完整路徑。如何告訴cakephp去someurl.com/action/var1/var2

這裏是我的控制器:

class ComplexesController extends AppController { 
    public $name='Complexes'; 
    public $uses=array('User', 'Complex', 'Unit'); 
    public $layout='pagelayout'; 



    public function view() { 
    $this->set('complex', strtoupper($this->params['id'])); 
    $c=$this->Complex->find('first', array('conditions'=>array('complex_name'=>$this->params['id']))); 
    $this->set('complex_data', $c); 
} 

}

,這裏是我的路線

Router::connect('/complexes/:id', array('controller' => 'complexes', 'action' => 'view')); 

我在哪裏寫調用特定單位的行動?在我的「觀看」行動或另一個名爲「單位」的行動內?我如何告訴蛋糕路線?

+0

我要補充一點,我做我的控制器創建一個函數調用單位'公共功能單元(){ \t \t \t \t $這個 - >設置( 'unitnum',strtoupper($這個 - > PARAMS ['身份證「])); ('first',array('conditions'=> array('unitnum'=> strtoupper($ this-> params ['id']))));}};} \t \t $ this-> set('unitdesc',$ u);`但不知道如何正確訪問它。我的所有模型關聯都正常工作,因爲我可以使用相同的關聯訪問其他控制器中的所有信息。 – huzzah 2011-12-16 21:39:36

回答

0

我想通了!我必須傳遞一個參數,在我的view()函數中調用$ unit,在那裏設置條件以查找我在url($ unit)中輸入的unitnumber。所以,我的代碼最終看上去像這樣:

public function view($unit=null) { 
    $this->set('complex', strtoupper($this->params['id'])); 
    $c=$this->Complex->find('first', array('conditions'=>array('complex_name'=>$this->params['id']))); 
    $this->set('complex_data', $c); 
    $u=$this->Unit->find('first', array('conditions'=>array('unitnum'=>$unit))); 
    $this->set('unit', $u); 
} 

,在我的路線文件添加此行:

Router::connect('/complexes/:id/*', array('controller' => 'complexes', 'action' => 'view')); 

和它的工作!