Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP, extensions and layouts

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.

like image 438
Rob Wilkerson Avatar asked Nov 14 '22 20:11

Rob Wilkerson


1 Answers

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.

like image 132
Rob Wilkerson Avatar answered Dec 11 '22 02:12

Rob Wilkerson