PHP code example of effectra / router

1. Go to this page and download the library: Download effectra/router library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

effectra / router example snippets




use Effectra\Router\Route;
use Effectra\Router\RouteGroup;

use Effectra\Http\Foundation\RequestFoundation;
use Effectra\Http\Message\Response;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

//pass response class to the router
$response = new Response();

$route = new Route($response);

$route->get('/home', function () {
    return 'Welcome to the home page!';
});

$route->post('/contact', 'StaticController@login');

$router->get('/', [HomeController::class, 'index']);

$router->get('/users/', [UserController::class, 'index']);


$router->post('/signin', function (RequestInterface $request, ResponseInterface $response) {
    return $response->json([
        'message' => 'hello world'
    ]);
});

$route->crud('/report', ReportController::class, 'read|readOne|create|delete|deleteAll|search');

$route->auth('/auth/', AuthController::class);

$route->group('/file/upload/', UploadController::class, function(RouteGroup $router){
    $router->post('audio','createAudio');
    $router->post('video','createVideo');
});


$request = RequestFoundation::createFromGlobals();

$route->dispatch($request);

$router->get('/users/{id}', [UserController::class, 'show'])->middleware(new AuthMiddleware());


$route->setNotFound(function () {
    // Handle 404 Not Found response
});

$route->setInternalServerError(function () {
    // Handle 500 Internal Server Error response
});