- Basic Concepts
- Files and Directory Layout
- Request Processing Overview
- The Application Object
- Modules
- Generating a Response
- URL Handlers
- Requests
- Responses
- Filters and Layout
- Dependency Injection
- Static Resources
- Common Patterns
- Settings
- Sessions
- Handling Logins
- Sendables
- Encryption
- Deploying Resources
- Asset Storage
- Logging
- Handling Exceptions
- Dates and Times
- Events
- Record Streams
- Modules and Scaffolds
- Http Clients
- Toolkit Classes
- StringTools
- Mime
- Xml
- Custard
- Introduction & Setup
- Creating a custard command
Generating a Response
To generate a response a class should implement the GeneratesResponseInterface
interface and define the
'generateResponse()' method.
UrlHandler
objects implement this interface however they usually instantiate a specialist class that generates
the real response.
The class should return a Response
object, normally a HtmlResponse
for web requests or a JsonResponse
for a
REST API.
A simple response generator might look like this:
class GreetingResponder implements GeneratesResponseInterface
{
public function generateResponse(Request $request)
{
$response = new HtmlResponse();
$response->setContent("<p>Welcome friend!</p>");
return $response;
}
}
To connect this with a URL you can use the ClassMappedUrlHandler
. For example here we configure it to
serve the homepage of our site:
// In registerUrlHandlers of your module class:
$this->addUrlHandlers(
[
"/" => new ClassMappedUrlHandler(GreetingResponser::class)
]);
Creating HTML directly in a response generating object is something usually reserved for very simple, non interactive use cases. For normal interactive screen design it's better to use a design pattern like MVP which you can find in the leaf module.