Unable to set up POST method route in CakePHP
-
13-07-2021 - |
Question
I've set up the following route in CakePHP 1.2:
Router::connect(
"/inbound/:hash",
array('controller' => 'profiles', 'action' => 'inbound', '[method]' => 'POST'),
array('hash' => '[0-9a-zA-Z]+'),
array('pass' => array('hash'))
);
Here are my request headers (via drupal_http_request()):
POST /inbound HTTP/1.0
Host: mysite.dev
User-Agent: Drupal (+http://drupal.org/)
Content-Length: 45
hash=test
However, I'm getting a 404 in response when I post. If I remove the parameter ":hash" from my route definition I get a 200. But in both cases the action in my controller does not get the passed parameter (hash).
I'm unsure what I'm doing wrong, as I appear to be doing what is in the doc.
Solution
The purpose of the pass
parameter is to define which route parameters are being passed to the action. So what you are doing there is creating a route that connects to URLs like this:
/inbound/foo
where foo
would be passed as a parameter to the controllers inbound
action.
Your request however points to /inbound
only, so this won't match your route as the parameter is missing, and consequently you are receiving a 404.
The data in the body of your POST request is being passed as regular POST data, ie it would be available via the controllers params
property:
$this->params['form']['hash']
So either remove the hash
parameter in the route and access the data via $this->params['form']
, or pass the data in the URL where the hash
parameter is defined:
/inbound/test
then you can access it in your controller action like this:
function inbound($hash)
{
echo $hash;
}