2012-05-01 144 views
1

我想創建我的第一個鋰電應用程序,並且出現一個非常奇怪的錯誤。鋰鏈接路由

我有我的索引視圖中的線,

<td><?php echo $this->html->link($question->title, array('controller'=>'questions','action'=>'view','id'=>$question->id)); ?></td>

其中我會想象會鏈接到該記錄視圖,但是使用'questions/view'.$question->id'它,使用數組網址我得到一個致命的。

Fatal error: Uncaught exception 'lithium\net\http\RoutingException' with message 'No parameter match found for URL( '控制'=> '問題', '動作'=> '查看', 'ID'=> '1').' in /Applications/MAMP/htdocs/learning-lithium/libraries/lithium/net/http/Router.php on line 306

這對我看起來像路由器試圖匹配在幫助器中的URL,因爲它不能,出於某種原因,它拋出一個異常。有沒有人有任何想法,爲什麼這是?我從CakePHP的角度攻擊鋰,所以這對我來說似乎很奇怪。

回答

1

'args' PARAM由默認路由處理並作爲參數傳遞給您的操作方法。

試試這個:

<?=$this->html->link($question->title, array('Questions::view', 'args' => array($question->id))); ?> 

要與id PARAM路線,你需要指定查找通過{:id}一個id PARAM的路線。查看「數據庫對象路由」部分的默認routes.php文件。這有一些例子,我將在下面爲複製完整性:

/** 
* ### Database object routes 
* 
* The routes below are used primarily for accessing database objects, where `{:id}` corresponds to 
* the primary key of the database object, and can be accessed in the controller as 
* `$this->request->id`. 
* 
* If you're using a relational database, such as MySQL, SQLite or Postgres, where the primary key 
* is an integer, uncomment the routes below to enable URLs like `/posts/edit/1138`, 
* `/posts/view/1138.json`, etc. 
*/ 
// Router::connect('/{:controller}/{:action}/{:id:\d+}.{:type}', array('id' => null)); 
// Router::connect('/{:controller}/{:action}/{:id:\d+}'); 

/** 
* If you're using a document-oriented database, such as CouchDB or MongoDB, or another type of 
* database which uses 24-character hexidecimal values as primary keys, uncomment the routes below. 
*/ 
// Router::connect('/{:controller}/{:action}/{:id:[0-9a-f]{24}}.{:type}', array('id' => null)); 
// Router::connect('/{:controller}/{:action}/{:id:[0-9a-f]{24}}'); 

所以,你會需要取消註釋取決於你的IDS採取何種格式的兩個部分之一。他們使用id參數的正則表達式來確保它不匹配不是id的url參數。順便提一下,第一條路線是將id的默認值設置爲null,這對我來說並不完全合理,因爲我不認爲路線會與空值匹配,但無論如何,這就是您如何設置默認值你的參數。

請注意,如果你這樣做,你的控制器操作方法必須是這樣的:

public function view() { 
    $id = $this->request->id; 
    // or an alternative that does the same thing 
    // $id = $this->request->get("params::id"); 
    // ... etc ... 
} 

得到作爲參數傳入到控制器的操作方法URL片的唯一方法是使用'args' PARAM 。

+1

非常感謝,這就是我一直在尋找。這是否意味着如果你在參數中傳遞'id',你需要一個帶有'/ {:id}'的路徑? –

+0

@DavidYell是的,這些路由在'app/config/routes'中,供您取消註釋(取決於您使用的是關係數據庫還是面向文檔的數據庫)。但如果符合您的需求,我建議您使用'{:args}'去。 – Oerd

+1

@DavidYell是的,沒錯。我更新了包含Oerd提及的信息的答案。 – rmarscher

0

你不是在你的路線使用命名參數,所以在你看來只是輸出如下:

<?php echo $this->html->link($question->title, array('controller'=>'questions', 'action'=>'view', $question->id));?> 

在QuestionsController你的函數簽名應該簡單地:

public function view($id) {} 
+0

如果你這樣做,你會得到同樣的錯誤,但'0'=>'32'' –