2011-05-06 80 views
2

爲了問我的問題,我需要解釋一下我的代碼第一...的Kohana 3.1.1路由問題jQuery的標籤

我有一個控制器(Controller_App)延伸Controller_Template。在控制器的模板視圖中,我有3個選項卡的jQuery選項卡。當我訪問URI:/view/26,以如下的路線踢:

Route::set('view_story', '<action>/<id>(/<stuff>)', array(
    'action' => 'view', 
    'id'  => '\d+', 
    'stuff'  => '.*', 
)) 
->defaults(array(
    'controller' => 'app', 
)); 

下面的函數,然後調用在Controller_App和設置的的URI「探索」 jQuery的標籤,並使其成爲默認的選擇:

public function action_view($id) 
    { 
     $this->template->controller['explore'] = Route::get('explore') 
      ->uri(array(
       'controller' => 'explore', 
       'id'   => $id, 
      )); 
     $this->template->default_tab = 2; 
    } 

這裏是我的 「探索」 路線:

Route::set('explore', '<controller>/<id>', array(
    'controller' => 'explore', 
    'id'   => '\d+', 
)) 
->defaults(array(
    'action' => 'index', 
)); 


問題:
當我嘗試訪問URL爲「myhost.com/view/26」的故事時,它設置了一切正常,但它認爲「/ view」是一個目錄,因此它會嘗試調用「 myhost.com/view/explore/26。由於沒有控制器稱爲「視圖」,我得到一個404錯誤。我能得到周圍的404錯誤通過創建以下路線:

Route::set('explore', '(<directory>/)<controller>/<id>', array(
    'directory'  => 'view', 
    'controller' => 'explore', 
    'id'   => '\d+', 
)) 
->defaults(array(
    'directory' => '', 
    'action' => 'index', 
)); 

...然後改變我的功能:

public function action_view($id) 
    { 
     $this->template->controller['explore'] = Route::get('explore') 
      ->uri(array(
       'directory' => '', 
       'controller' => 'explore', 
       'action'  => 'index', 
       'id'   => $id, 
      )); 
     $this->template->default_tab = 2; 
    } 

但加載頁面時,它調用jQuery.get (),但它試圖調用「/ view」目錄下的PHP文件而不是當前目錄。

我不知道這是一個簡單的路由問題,或者如果我什至吠叫了正確的樹。但我已經嘗試了所有不同的路線組合,並且不能爲我的生活弄清楚這一點。所有的建議表示讚賞!

感謝, 布賴恩

+0

你說當你導航到'myhost.com/view/26'時,一切正常,但它會嘗試調用'myhost.com/view/explore/26'?什麼試圖調用'myhost.com/view/explore/26'?也許你需要在路由輸出中包裝'URL :: site()'? – 2011-05-06 10:22:28

+0

@davgothic - 對不起,昨天晚上我發佈這個時候已經很晚了。 ;)在action_view函數中(第一個函數),它爲探索控制器構建一個路由。但是當它做到這一點時,它會保留'/ view'目錄並將其放在我的路線上。如果我修改了路由定義並修改了我的'action_view'函數(如第二個例子所示),它解決了這個問題,但是我的AJAX調用混亂了,因爲它不是調用'core/stories',而是調用'/ view /核心/ stories'。這就像我需要改變我目前的工作直接上一級... – DondeEstaMiCulo 2011-05-06 20:02:51

回答

0

Route::uri(...)產生的URI這不是絕對的。 切換到使用Route::url(...),你應該很好去。

Route::url(...)是通過Route::uri(...)URL::site(...)的快捷方式。

public function action_view($id) 
{ 
    $this->template->controller['explore'] = Route::get('explore') 
     ->url(array(
      'controller' => 'explore', 
      'id'   => $id, 
     )); 
    $this->template->default_tab = 2; 
} 
+0

問題是當從我的外部JS文件內進行AJAX調用。我會在哪裏放置Route/URL方法? – DondeEstaMiCulo 2011-05-14 01:18:04

+0

我的預感是AJAX代碼已經被賦予了一個相對的URI,而不是絕對的(即'foo.html'而不是'/ foo.html'),因此代碼試圖從當前的代碼中找到一個dir網址,然後添加相對的網址。在你做這個任務的地方改變它:'$ this-> template-> controller ['explore'] = ..' – Lethargy 2011-05-15 21:39:42