Micro REST API framework written in pure PHP for micro and small projects. Create rest api blazing fast with JWT authentication.
- Fast and easy working with database
- Clear routing (get request -> check permission -> run controller)
- Incredibly simple permissions
- Clone repo and run composer install.
- Go to:
/config/database.php
and edit your connection with database - Run
php -S localhost:8000
- Send GET Request at
localhost:8000/setup
That last point will create table for users (required for authorization). Now you have working REST API backend with simple functions. Read small documentation bellow to understand how to use PHP Rose.
First of all, that framework aiming for simplicity. You can use only GET and POST.
- GET - For returning general information, without user interactions and requests
- POST - For messing with backend, returning specific informations (eg. about user)
In file router.php
in home folder. You can use get()
and post()
functions.
- Firsth parameter is your route for request.
- Second parameter is your controller which will run after visit that route.
- Third parameter is not required, it's permission parameter. If not given, route will be allowed to visit for guests (authorization for that route will be disabled). That effect can be achieved with sending 0 as third parameter.
For code clarity controllers are held in folder /controllers
. Just include_once group of controllers to router.php
.
get('/example_route','example_controller',1);
Sending GET request at localhost:8000/example_route
will trigger function example_controller
and that route will be available only for users with permission 1
.
post('/second_example_route','second_example_controller');
Sending POST request at localhost:8000/second_example_route
will trigger function second_example_controller
and that route will be available for all users (have permission 0).
In folder controllers
. You can create how much you want and organize however you want. In file (group of controllers eg. controllers/general.php
) you can have how much controlers you want.
Controllers are just a functions.
function example_controller(){
response([
'message'=>'This is example response'
],200);
};
Controller will return JSON response with status code 200. Read more about functions in PHP Rose.
Before entering into controller user is verified. If route required permission user token is checked (go to fast configuration to configure permissions but firsth read that section). Permissions are realy simple, they are just numbers from 0.
Recommended way to look at this:
- 0 - is for guest, all users having link will have permission to view that route
- 1 - is for admin
- 2 - is for user
- 3 - is for eg. smaller user
User with permission 2 can view all routes for permission 2,3,4,5 etc. But can't view route with permission 1. The same applies to user with permission 3, user can't view routes with permission 1 and 2 but can view 3,4,5 etc. Routes with permission 0 are available for all users and guests (not logged users, without token)
Token is sending in Headers. Set header name at Token.
response()
- For fastest creating responses. At firsth parameter insert array (later will be processed to JSON) and at second parameter insert response status code (not required, not given will return status code 200).
response([
'message'=>'This is example response'
],200);
return_404()
- For custom 404. It's not required function. It's overwrites PHP Rose default 404. Use only once, recommended it's to put this function in controllers/general.php
DB::query()
- For requests to database. In firsth parameter just type SQL query. Second argument is optional and handle params. Examples can be found in /controllers/general.php
but in general use something like:
DB::query("SELECT * FROM users")
or DB::query("SELECT * FROM users WHERE id=:id",['id'=>$id])
- Go to
system/auth.php
and edit secret_key for JWT authorization. You can edit token_exp if you wanna choose how long token will be valid. - Send request at
localhost:8000/register
and create firsth user (use POST request with login(text),password(text) and permission(int)) - Send request at
localhost:8000/login
and login your user (use POST request with login(text) and password(text)) - Set recived token in headers and for header name give Token
That's all. Now you can delete examples in code and setup function (controller can be found in /controllers/general.php
and route in router.php
)
- config - config files, you can add there important things
- controllers - controllers for routes, you can add how much you want and organize however you want
- system - better do not touch if not necessary
- index.php - be careful with editing
- router.php - feel free, here you create routes
- system/router.php - routing heart, handling requests
- system/auth.php - authentication heart, handling permissions
- controllers/general.php - general controller for general controllers
- controllers/users.php - controller for authorization (register new user and login)
- config/database.php - database connection