Skip to content

Nelson-Dominici/Doorsphp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸšͺ Doorsphp πŸšͺ

πŸ„ About

Doorsphp is a framework created for student purposes, with no intention of being used in real projects.

πŸ—Ί Routes

Doorsphp has resources to create HTTP Routes, using the methods of the App class object, the methods are: get(), post() , put() and delete(), each representing their respective HTTP methods, these methods must receive two arguments, the first is the endpoint of the route and the second is the controller of the route.

<?php

require_once("./vendor/autoload.php");

use Doorsphp\App;
use Doorsphp\Route\Request\Request;
use Doorsphp\Route\Response\Response;

$app = new App();

class ExampleClass
{
    public static function exampleMethod(Request $req, Response $res): void 
    {
        $queryParams = $req->queryParams;
        $urlParams = $req->urlParams;

        var_dump($urlParams);
	
        $res->status(200);
    }
};

$app->get("/endpoint/:urlParam", [ExampleClass::class, "exampleMethod"]);

$app->post("/endpoint", function(Request $req, Response $res): void
{
    $queryParams = $req->queryParams;
    $body = $req->body;

    $res->status(200)->sendJson(["success" => true]);
});

πŸ•Ή Route controller

There are two ways to place a controller in a route:

  • Using an array with a Class with the identifier "::class" and a string with the name of the method(method must be static):

  • $app->get("/endpoint/:urlParam", [ExampleClass::class, "exampleMethod"]);
  • Using an anonymous function:

  • $app->post("/endpoint", function(Request $req, Response $res): void
    {
        $queryParams = $req->queryParams;
        $body = $req->body;
    
        $res->status(200)->sendJson(["success" => true]);
    });

🎲 Dynamic routes

Doorsphp has a feature for creating dynamic routes, to create them just create a route with the root of the route path with ":":

$app->get("/endpoint/:urlParam", [ExampleClass::class, "exampleMethod"]);

πŸ“¦ Request Classs

In the Request Class, which is the first parameter of the Controller, you will have access to the data of: Request Body, Url Params and Query Params, Middleware Data:

$body = $req->body;
$urlParams = $req->urlParams;
$queryParams = $req->queryParams;
$middlewareData = $req->middlewareData;

πŸ—³ Response Classs

In the Response class, which is the second parameter of the Controller, you will have access to the sendJson() method to send a json as a response, and to the status() method to send the response status (can you call both at the same time, or just one).

$res->status(200)->sendJson(["success" => true]);

πŸ”’ Middlewares

  • To return data from one middleware to another, or to your "final" controller, you simply return an associative array (key and value)
  • If you call any method of the Response Class in a middleware, the execution of the following middlewares or the "final" controller will be interrupted.

It is possible to apply middlewares to routes, using one or more controllers before your "final" controller.

class FirstMiddleware
{

    static public function execute(Request $req, Response $res): void
    {
       // Firs Middleware
    }
	
}

$app->get("/endpoint", 

    [FirstMiddleware::class, "execute"],

    function(Request $req, Response $res): array
    {
        return ["example" => "12345"];
    },

    function(Request $req, Response $res): void
    {
        var_dump($req);
    }
);

βš™ Requirements

  • PHP version =>8.1.13
  • composer version =>2.5.1

πŸ”₯ Author


Nelson Dominici

About

πŸšͺ Doorsphp - A PHP Framework πŸšͺ

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages