Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7578753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:30:15+00:00 2026-05-30T17:30:15+00:00

I have set up REST for my CakePHP and I’m having a slight problem.

  • 0

I have set up REST for my CakePHP and I’m having a slight problem. When calling a GET method, like view or index on my controller, or even a custom method that is GET I have no problem getting an answer. But when I try to do a POST action like add I get no output from the operation even though I can see that it reached it correctly and executed (saved to the DB).

I have properly set up the layout file for JSON and XML output as well as the routing and the view for each output type.

EDIT:

Relevant code in beforeFilter in AppController:

      if ( $this->RequestHandler->isAjax() ) {
        $this->layout = 'ajax';
        $this->autoRender = false;
      } elseif  ($this->RequestHandler->isXml()) {
        $this->layout = 'default'; 
        $this->RequestHandler->respondAs('xml');
        $this->RequestHandler->renderAs($this, 'xml');
      } elseif ($this->RequestHandler->ext == 'json') { 
        $this->layout = 'default';
        $this->RequestHandler->respondAs('json');
        $this->RequestHandler->renderAs($this, 'json');
      } elseif ($this->RequestHandler->accepts('html')) {
        $this->layout = 'frontend'; 
      }

Code in Routes:

Router::mapResources('fonykers');
Router::mapResources('voicenotes');
Router::parseExtensions();

Relevant code in my add method in FonykersController:

$response = array('ok' => true, 'title' => 'Thank you for registering', 'msg' => 'A confirmation email has been sent to the provided email address, please click on the link in the email to complete the registration process');
if ($this->RequestHandler->isXml()) {
  $this->set(compact('response'));
} else if ($this->RequestHandler->ext == 'json') {
  pr('This prints');
  $this->set(compact('response')); //View not being outputted
  pr('This also prints');
} else {
  echo json_encode($response); 
} 

My view in /views/fonykers/json

<?php echo json_encode(array('response' => $response)); ?>

My layout file for json:

<?php 
header("Pragma: no-cache"); 
header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate"); 
header('Content-Type: application/json'); 
header("X-JSON: ".$content_for_layout); 
echo $content_for_layout; 
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:30:15+00:00Added an answer on May 30, 2026 at 5:30 pm

    You have a few things going on…

    Your beforeFilter checks for isAjax and then uses elseif to test for XML or JSON. If your request is AJAX, it will never get to the XML/JSON tests. Your layout is likely not being set at all which leaves you with just $this->autoRender = false;.

    Since autoRender is disabled, at some point you’ll have to manually invoke rendering. Using $this->set() just prepares variables for usage in your view – it doesn’t actually output the view. In your controller action, the only line that would actually output anything is echo json_encode($response);.

    Use $this->render() to force Cake to render the view when you want it to. More info in The 1.3 Book: Rendering a specific view


    As an aside, sometimes you use ...}elseif(... and sometimes you use ...}else if(.... This isn’t technically wrong, but being consistent will make your code easier to read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have successfully set up a quick test of creating a REST-like service that
We have set up a system where notifications get sent to a user with
I have set up a REST API using the Slim PHP framework, which works
I have set my compression like this for my NSMutableUrlRequest on my iphone app
I have the following problem: Let's consider we have #define SET callMe #define COLUMN(x)
I have a json array in my Particles controller that looks like this after
I have set up my REST service and it works fine. This is the
I have basic authentication set up in a simple CakePHP 2.0 application. I first
Am using phil sturgeon codeigniter REST library for my REST api, i have set
I have set a variable in my url wich i get in my next

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.