Controllers\Cart
methodを持つ があるとしましょうpostAdd()
。このメソッドは、POST リクエストを に送信すると呼び出されますhttp://www.site.com/cart/add
。これはショッピング カートに商品を追加するためのコントローラー メソッドなので、明らかにproductId
が投稿されます。
コントローラーでこれを行うと、すべての POST データが文字列として表示されるため
public function postAdd() {
$productId = $this->request->post('productId'); // It is of a 'string' type.
// You would then probably do something like...
$this->shoppingService->addToCart($productId);
.........
}
のインターフェースは次のようにServices\Shopping
なります
interface ShoppingInterface {
/**
* @param int $productId
* @return bool
*/
public function addToCart($productId);
}
PHP は緩く型付けされているため、そのメソッドに文字列を渡すことができますが、最初にデータを整数にキャストする必要がありますか?