Search code examples
cakephppostrequestrequesthandler

cakePHP: / redirects to /index but sends a get instead of post request


im totally confused right now, because i want to build up an api, but cake has a bit of a weird behaviour:

If i want to send a POST-request to let's say "/controller", it gets redirected to "/controller/index" ("index" method of "controller"), which is quite what i did expect. But, instead of a POST-request, i get a GET-request there.

(for example, i output $this->request->method(), which prints out the method of the request)

Someone who came across that before? I tried routing the "/controller" to "/controller/index" in routes.php under "Config", but that didn't work as well.

Many thanks,

Fabi.


Solution

  • what you're describing isn't how cake works. A Post request to /foo is not redirected to /foo/index by any core code. Also, by default both of those urls map to the same controller action: index.

    Finding the problem

    You can easily identify redirect problems by temporarily putting this in your app controller::

    function redirect($url) {
        debug($url);
        debug(Debugger::trace());
        die;
    }
    

    This will dump a stacktrace indicating how the code is getting to the redirect method when it is called. Once you know what is responsible for redirecting /controller to /controller/index, you'll be able to focus your attention on the real problem.

    Note that obviously any kind of redirect will "convert" your post request to a get request - since you can't issue a redirect and submit post data at the same time from php.