A simple router to use in PHP API's
You need the .htaccess
file existing on the repo to redirect all your requests to the index.php
file.
Create a folder controllers
in the same folder of index.php
file and create your controllers inside.
- All your controllers must have the route name capitalized (e.g. "User")
- All your class names on the controller files must end in the word
Controller
(e.g. "UserController").- All your methods must start with the word
action
(e.g. "actionIndex")
Route: "https://example.domain.com>/user"
class UserController { // class name ending with the word "Controller"
public function actionIndex() // method name starting with the word "action"
{
return "User Controller";
}
public function actionGet($id)
{
return (object)[
'id' => 1,
'name' => 'John Doe'
];
}
}
"https://example.domain.com/user"
"User Controller"
"https://example.domain.com/user/get/id/1"
{
"id": 1,
"name": "John Doe"
}