different ways depending on how its called.
For instance assume you have a controller called Foo with an action
called bar that returns true in JSON or XML or renders a view
depending on how it was requested.
class FooController extends AppController {
function bar() {
$this->data = true;
if ( $this->isAPICall() ) {
$this->renderAPI();
} else {
$this->render();
}
}
class AppController extends Controller {
public function isAPICall() {
if ( $this->RequestHandler->accepts('html') ) {
return false;
}
if ( $this->RequestHandler->accepts('xml') || $this->RequestHandler-
>accepts('json') ) {
return true;
}
return false;
}
public function renderAPI() {
// Turn off debugging so the render time does not get included in
the output
Configure::write('debug', 0);
if ( $this->RequestHandler->accepts('xml') ) {
$this->renderXML();
}
elseif ( $this->RequestHandler->accepts('json') ) {
$this->renderJSON();
}
}
}
This works great and allows me to re-use tons of logic. The problem is
how to test it using the Cake 1.3 test cases. If there where some way
to modify request headers before the action is called it would be a
easy as cake :)
Anybody think this is possible if CakeTestCase was modified?
Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
No comments:
Post a Comment