drd2551 2011-12-22 09:30
浏览 13

CakePHP - REST POST功能无法使用默认映射

I am new to CakePHP and am attempting to configure my web app to use REST.

I am using CakePHP version 2.0.4.

Following the CakePHP Cookbook, I have entered the following into the routes.php file ('Apples' pointing to the ApplesController.php controller):

Router::mapResources('Apples');
Router::parseExtensions();

and in the controller itself (ApplesController.php), I have added the following component:

public $components = array('RequestHandler');

Also in the controller, I have an add action which echo's a string (as follows):

function add() {
    echo 'The add action has been called';
}

Finally, I created a view (index.ctp located in app/View/apples/) which uses a HTML form with the method POST and the action "/apples/" to submit.

<h2>Home<h2>
<form action="/apples/" method="post">
   <input type="submit" value="Submit" />
</form>

Now according to the the CakePHP Bakery (http://bakery.cakephp.org/articles/rightwayindia/2010/01/11/restful-web-application-development-in-cakephp), the mapResources should automatically map POST to the 'add' action of the controller specified in the routes.php file, therefore when the button is clicked, should echo the string in the add action.

This only works when you change the action parameter in the HTML (in index.ctp) to:

<form action="/apples/add" method="post">

and explicitly point to the add action.

I may be wrong, but I thought that by configuring REST in the routes.php file should automatically map the specific REST methods to the actions such as add, edit, delete etc.. (stated on the web page linked above) to the controller stated in the paramater of the mapResources function.

I have also tried custom REST routing but this is also not working. However, It would be nice for it to work with default mapping rather than customising it.

Any suggestions would be appreciated. Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图