-
Notifications
You must be signed in to change notification settings - Fork 0
02_controllers__03_response
The response class is responsible for sending output to the browser.
An instance of the response object is always available in controllers and it can be accessed like this:
$this->response
The body method can be used to override the response body in the after method of your controller.
public function after()
{
// Will prefix the response with "Hello, world!";
$this->response->body('Hello, world!' . $this->response);
}
The status method is a helper method that makes it easy to send HTTP status headers. The status method will always reply using the same protocol version as the request.
$this->response->status(404); // Sends the 404 "Not Found" header.
The type method lets you set the content type of your response. The default charset of the application is used if the second parameter is left empty.
$this->response->type('application/json');
The redirect method allows you to redirect a user to a different URL. The default status sent by the method is 302 Found but you can override this by setting the status code in the second parameter.
// Redirects to http://example.org/foo/bar
$this->response->redirect('foo/bar');
// Redirects to http://example.com with the "301 Moved Permanently" header
$this->response->redirect('http://example.com', 301);
The filter method allows you to filter the output before sending it.
$this->response->filter(function($body)
{
return str_ireplace('[mako:assets]', Assets::location(), $body);
});
- Getting started: Installation
- Getting started: Configuration
- Getting started: Error handling
- Getting started: Constants
- Getting started: Coding standards
- Controllers: Controllers
- Controllers: Request
- Controllers: Response
- Databases: Basics
- Databases: Query builder
- Databases: Orm
- Databases: Migrations
- Command line: Basics
- Command line: Custom tasks
- Packages: Basics
- Packages: Composer packages
- Learn more: Array helper
- Learn more: Asset manager
- Learn more: Autoloading
- Learn more: Caching
- Learn more: Cookies
- Learn more: Events
- Learn more: File helper
- Learn more: Html helper
- Learn more: Internationalization
- Learn more: Logging
- Learn more: Number helper
- Learn more: Pagination
- Learn more: Redis client
- Learn more: Rest client
- Learn more: Security helpers
- Learn more: Sessions
- Learn more: String helper
- Learn more: Url helper
- Learn more: Uuid helper
- Learn more: Validation
- Learn more: Views