Início / Optitravel / REST API
Creating Your Routes
Introduction
Routes are the backbone of any MVC-based application. They determine how HTTP requests are directed to specific controller methods, defining the connection between a URL and the functionality it should trigger.
In this framework, routes are declared in a centralized array, making it easy to register, manage and protect endpoints. Whether you’re building a public login API or a protected user management system, defining routes clearly is essential to your application’s structure.
Where to Create?
All routes should be defined in a single PHP file, typically named routes.php, located in the export/rest/v2/ directory. Each route must follow a consistent format to ensure it can be properly processed by the framework's internal router.
How to Create?
Each route is defined as an associative array with four main keys:
method— the HTTP verb (e.g.,GET,POST,PUT,PATCH,DELETE).path— the URL path that this route matches. You can include dynamic parameters using{}.handler— the controller and method to be executed when the route is matched. Use the format\Namespace\Class->methodfor instance methods orClass::methodfor static ones.auth— a boolean that defines whether Bearer Token authentication is required.
Example:
[
'method' => 'POST',
'path' => '/scim/v2/Auth/Login',
'handler' => '\App\system\login\Auth\RestAuth::generateAuthToken',
'auth' => false
]
Best Practices
Here are some recommendations for managing your routes effectively:
-
Group related routes together — This improves readability and helps maintenance.
-
Use meaningful paths — Routes should reflect their purpose (e.g., /User, /Login, /Product/{id}).
-
Keep authentication consistent — Protect sensitive routes with auth: true.
-
Avoid overly complex routes — Each route should correspond to a single responsibility or resource.
↑ (Última atualização: 06/05/2025)