1

In CakePHP 1.3 I have this router in routes.php:

Router::connect('/listing/*', array('controller' => 'items', 'action' => 'index'));

My question is, how can I create an html-form with submit action to www.mysite.com/listing.json?

This is the form I currently have in the folder app/views/items/index.cpt.

$this->Form->create( 'Item' ); // HTML: <form action="/listing" method="post">

I tried to change the submit-action to "listing.json", but it didn't work because "/items/" is being prepend, and I don't want that.

$this->Form->create( 'Item', array( 'action' => 'listing.json' ) ); // HTML: <form action="/items/listing.json" method="post">

++

In other words, I want the form action to be like this: any idea?

<form action="listing.json" method="post">
4

1 回答 1

1

First you will need to configure your router to handle .json.

Router::parseExtensions('json');

Then do the form creation like this.

$this->Form->create( 'Item', array('url' => array('controller'=>'items', 'action'=>'index', 'ext' => 'json')));

I believe this would work.

But... There's a problem with what you are trying to do. Even though you mark the submit URL as a *.json, it doesn't mean you will be posting a json request. With this change, the only thing you would be doing is posting a html form to a url named as a *.json.

于 2012-05-15T14:50:41.053 回答