Skip to content

tobioye88/tiny

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tiny Framework

Version 2.1.0

Tiny framework is a quick starting point for bootstrapping PHP web apps. Tiny framework was built with Developers in mind. This makes it easy for developers to quickly create APIs and web apps

// Hello World

$app->get('/', function(IRequest $req, IResponse $res){
    $res->json(['response' => 'Hello, World!']);
});

OR

// Hello World

$app->get('/user', [UserController::class, 'getUser']);

Application Route

Routes are created in the routes file located in the app/routes/route.php

Available Methods are GET, PUT, PATCH, POST, DELETE, OPTION

$app->get('/', function(IRequest $req, IResponse $res){
    $res->json(['response' => 'Hello World']);
});
$app->put('/', function(IRequest $req, IResponse $res){});
$app->patch('/', function(IRequest $req, IResponse $res){});
$app->post('/', function(IRequest $req, IResponse $res){});
$app->delete('/', function(IRequest $req, IResponse $res){});

Application Views

Views are located in the app folder src/Views/index
Use the variables in the view file like they were declared in the file;

$app->get('/', function(IRequest $req, IResponse $res){
    $res->view('index');
});

// with data

$app->get('/', function(IRequest $req, IResponse $res){
    $numbers = [1,2,3,4,5];
    $name = 'John Doe';
    $res->view('index', compact('numbers', 'name'));
});

Path Parameter

$group->get('/posts/{id}', function(IRequest $req, IResponse $res){
    $id = $req->getPathParam('id');
    $res->json(['post_id' => $id]);
});

Query Parameter

// Route http://localhost/posts?id=1

$group->get('/posts', function(IRequest $req, IResponse $res){
    $id = $req->getQueryParam('id');
    $res->json(['post_id' => $id]);
});

Routes Group

For grouping routes
eg:

  • /admin/posts
  • /admin/posts/comments
$app->group('/admin', function($group){
    $group->get('/posts', function(IRequest $req, IResponse $res){});

    $group->get('/posts/comments', function(IRequest $req, IResponse $res){});
});

Middleware

All middleware must implement the interface IMiddleware
eg:

class AuthorizedMiddleware implements IMiddleware {
    public function handle(IRequest $req, IResponse $res){
        $token = $req->getHeader('token');
        // Check if the token is valid
        if(!JWT::verify($token, JWT_SECRET)){
            throw new HttpUnauthorizedException("Unauthorized Request");
        }
    }
}

When validation for middleware fails throw an exception to interrupt the flow.

There are three levels at which middleware can be applied

  1. Application level
  2. Group level
  3. Route level

Application Level Middleware

$ipMiddleware = new IPMiddleware();
$app->middleware([$ipMiddleware]);

Group Middleware

$authMiddleware = new AuthorizedMiddleware();

$app->group('/admin', function($group){
    $group->get('', function(IRequest $req, IResponse $res){});
}, [$authMiddleware]);

Route Middleware

$authMiddleware = new AuthorizedMiddleware();

$app->get('admin/users', function(IRequest $req, IResponse $res){}, [$superAuthMiddleware]);

// OR

$authMiddleware = new AuthorizedMiddleware();
$routeMiddleware = new RouteMiddleware();

$app->group('/admin', function($group){

    $group->get('', function(IRequest $req, IResponse $res){}, [$superAuthMiddleware]);

}, [$authMiddleware]);

Sending Emails

$app->get('/email', function (IRequest $req, IResponse $res) {

        $result = Email::builder()
                ->from('email@localhost')
                ->to('tobioye88@yahoo.com')
                ->subject('Hello There')
                ->body('Hello, World!')
                ->send();

        $res->json(["isSent" => $result]);
    });

NB

Some coming Changes even though I know it's an old project.

  1. Handler Routing more efficiently
  2. Handle environment var instead of hard coding in the config class

About

A tiny php framework for handling http request

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages