I have a controller method that has long been handling JSON requests by parsing that extension, but now I need to open it up to cross domain ajax so I'd like to offer a JSONP variant by parsing that extension as well. I've already updated my routes.php
file:
Router::parseExtensions( 'json', 'jsonp' );
So far all is well, but the happiness ends when the results are rendered. While the .json
extension automagically picks up the json/default.ctp
layout, the .jsonp
content continues to adopt the non-specific default layout (and all of its unnecessary HTML content). I've tried using RequestHandler::setContent()
to set the response content type to both json
and js
, but that doesn't seem to be what triggers the call to a given layout directory.
Does anyone know what does determine which content-specific layout directory is called? I tried creating jsonp/default.ctp
and I've tried creating a js/default.ctp
layout with my JSONP result, but nothing seems to engage. I just get the normal default.
Any insight into how extensions/content type are mapped to these layout directories would be much appreciated.
I've temporarily solved this by explicitly setting the layoutPath
value:
$this->layoutPath = $params['url']['ext'];
This feels like one of those things where there must be a better solution, but maybe this is it. I'm going to leave the question open for a bit in the hopes that someone else has a solution that involves Cake's automagic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With