PHP code example of marcosdipaolo / router

1. Go to this page and download the library: Download marcosdipaolo/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/ */

    

marcosdipaolo / router example snippets




// instantiate the container
$container = new MDP\Container\Container();

// bind whatever you need
$container->set(
    MyAbstract::class, 
    MyConcrete::class
);

// create the Router instance passing the controllers and the container
$router = Router::create([ControllerClasses::class], $container);

// direct the request to the rigth place
$router->direct();



namespace MDP\Test\Router;

use MDP\Test\Router\MockedService\MockedServiceApi;
use MDP\Router\Attributes\Get;

readonly class ControllerClass
{
    public function __construct(private MyAbstract $service)
    {
        //
    }

    // We decorate methods with Verb Attributes
    #[Get("/")]
    public function mockMethod(): bool
    {
        return true;
    }
}
apacheconf
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]