Skip to content

MarcelHekkert/SimpleRouter-php

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 

Repository files navigation

SimpleRouter

See full example project: https://github.com/FrederickMontiel/CRUD-SimpleRouter-php

Install our package with composer:

composer require easy-projects/simple-router

You must redirect all requests to your main file with this .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

Example code:

<?php
    include_once __DIR__."/vendor/autoload.php";

    use EasyProjects\SimpleRouter\Router as Router;

    $router = new Router();
    
    //Cors rules for the broser
    $router->cors()->setAllowedOrigins("easyprojects.tech", "localhost");
    $router->cors()->setAllowedMethods("GET", "POST", "PUT", "DELETE");
    $router->cors()->setAllowedHeaders("Content-Type", "Authorization");
    
    /*
        Import only the packages and classes that you are using.

        Improves application loading performance.

        For this you need to gestione your project by packages.
    */
    $router->autoload(); 
    
    //Routes
    $router->get("/get/{idUser}", 
        //Middlewares for example
        fn () => Middleware::auth(),
        //Controller
        fn () => Router::$response->status(200)->send(Router::$request->params->idUser)
    );
    
    $router->post("/add", 
        fn () => Router::$response->status(200)->send(Router::$request->body->nameUser)
    );

    $router->put("/update/{idUser}", 
        fn () => Router::$response->status(200)->send(Router::$request->params->idUser." - ".Router::$request->body->nameUser)
    );

    $router->delete("/delete/{idUser}", 
        fn () => Router::$response->status(200)->send(Router::$request->params->idUser." - ".Router::$request->body->nameUser)
    );
    
    //Use only in development
    $api->start();

Now if you need get Files Uploaded, use:

    $router->post("/upload/folder/{idFolder}", 
        fn () => Router::$response->status(200)->send(Router::$request->files->img->name." - ".Router::$request->params->idFolder)
    );

image

How does it work?

It consists of 3 classes, Router, Request, Response.

Router is the global class to be able to design the routes, you can put different methods: get, post, put, delete. Request contains the data sent by the client's browser or the client in general, (url or form parameters, and headers). Response contains two methods, status to send a response code (404, 500, 403), send () send a response to any object in a json.

About

The faster way for create php routes.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%