Controller Response Types

Alloy controllers have a few useful response types that can be returned directly by the requested controller method. Note that since the response content is explicitly returned, if your controller method does not return anything, nothing will be displayed.

String Content

Returning raw strings is most useful for simple API responses that don't need their own templates. By returning a string directly, you can avoid the overhead of including and parsing another template or redundant formatting and markup code that might be unnecessary for simpler tasks.

// Simple strings return "Welcome " . $username; // Simple JSON API Responses return json_encode($event); // Custom user-provided XML serializer class or function return JavaAndDotNetAreCool::serializeXml($user, array('username', 'email'));


View Objects

There is a convenience function on the controller named 'template' that returns an instance of \Alloy\View\Template and sets the local module path and response format for you automatically. Read Alloy Views for more details on specific methods and functionality.

// Simple template return $this->template('indexAction'); // Template with variables passed return $this->template(__FUNCTION__) ->set(compact('posts', 'user', 'page'));


Resource Objects

The Resource object is a flexible response type provided primarily for web service responses. It accepts an array of data or an object that has a toArray method, and will transform the data to a JSON response (XML coming soon). As an extension of the abstract module response, it also provides methods to set an HTTP status code, errors, and more.

// Create and save new post $post = $kernel->mapper()->get('Module\Blog\Post'); $post->data($request->post('post')); // Gets $_POST['post'] if($kernel->mapper()->save($post)) { // Success (201 "Created" response) return $this->resource($post) ->created($post->getUrl()); } else { // Validation errors (400 "Bad Request" response) return $this->resource($post) ->status(400) ->errors($post->errors()); }


Any object with a __toString method

Any object that can be converted to a string (ala __toString) will be properly rendered and handled by Alloy. This allows you to create and use your own (or other) code libraries for forms, view partials, whole template engines, or whatever else you need. Since nothing is already loaded for your controller, there is no waste of unused resources or code.

// Example Zend_Form object $form = new Zend_Form($userFields); $form->setAction('/user/login') ->setMethod('post'); return $form;


Boolean False

Boolean false is a special return type that will render a generic 404 "File Not Found" HTTP response that will match the one served by Alloy for modules or actions that do not exist. In the context of a nested sub-request, a boolean false controller response will not render any content, and the primary request (and other nested sub-requests) will continue as normal, un-interrupted.

// Find post entity with given $id $post = $kernel->mapper()->get('Module\Blog\Post', $id); if(!$post) { return false; } return $this->template(__FUNCTION__) ->set(compact('post'));



Exceptions thrown anywhere within controller requests or nested sub-requests will stop execution and trigger an HTTP response unless caught in a try/catch block.


Triggers a 404 "File Not Found" HTTP response with the exception message set as the response body.


Generic and flexible way to trigger a desired HTTP response. Issues a HTTP response with the exception message set as the response body, and the exception code set as the HTTP response code.


Indicates the requested action requires authentication. Forces a user login and issues a 401 Unauthorized HTTP response.

Related Links