Controller
Snappy uses a semi-auto routing system, removing the need for a separate routing file. Instead, the URL endpoint is automatically mapped to a controller based on its filename, allowing for a clear and intuitive structure. When a request is made to a given URL —for example /products— the framework automatically looks for a controller file named products.php. This means the naming of your controller files directly determines the base URL path for accessing that controller’s logic.
Within the controller, you define the behavior for different types of HTTP requests by creating public functions, keeping your endpoint logic self-contained and easy to manage. This convention-based approach simplifies routing and reduces boilerplate, making your API structure more maintainable as it grows.
ℹ️ NOTE
Follow the RESTful API resource naming principles of being clean, clear, and simple.
Creating controller
How to create a controller:
Manual
- Define
namespace Controller - Define
use Core\Controller - Create class name using PascalCase and extends your class from
Controller - Save it with the same class name using PascalCase under controller directory
CLI
php snappy generate controller:YourControllerName --with-methods:get,post,put,patch,deleteExample:
// controller/YourControllerName.php
namespace Controller
use Core\Controller
class YourControllerName extends Controller{
}Methods
Snappy will execute the method inside the controller that matches the HTTP request method used in the request such as get() for GET requests, post() for POST requests, and so on. This convention allows you to define clean and organized RESTful endpoints without the need for manual routing configuration.
💡 Usage
GET method handler
public function get(): voidPOST method handler
public function post(): voidDELETE method handler
public function delete(): voidPUT method handler
public function put(): voidPATCH method handler
public function patch(): voidExample:
// controller/YourControllerName.php
namespace Controller
use Core\Controller
class YourControllerName extends Controller{
public function yourRequestMethod(){
}
}public public function yourRequestMethod() is the name of the request methods act as a function.
ℹ️ NOTE
Write only the method handlers you need.