PHP code example of mrsems / router-php

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

    

mrsems / router-php example snippets





re 'controllers/PageController.php';
 'routes.php';
$middlewares = file_exists('middlewares.php') ? 


function accueil()
{
    


tion afficherClient(array $params)
{
    $client = trouverClient($params['id']);

    


function trouverClient($id)
{
    return [
        'id' => $id,
        'nom' => 'Client exemple',
    ];
}


return [
    '/' => [
        'function' => 'accueil',
        'methods' => ['GET'],
    ],
    'client/{id}' => [
        'function' => 'afficherClient',
        'middlewares' => ['auth'],
        'methods' => ['GET'],
    ],
];


return [
    '/' => 'accueil',
    'client/{id}' => 'afficherClient',
];

<h1>Bienvenue sur la page d'accueil</h1>

<h1>Client <?= htmlspecialchars($client['id']) 


function verifierAuth()
{
    if (!isset($_SESSION['user'])) {
        header('Location: /login');
        exit;
    }
}

return [
    'auth' => 'verifierAuth',
];


return [
    '/' => [
        'file' => 'views/accueil.php',
        'methods' => ['GET'],
    ],
];


App\Router\Router;

$routes = ares.php') ? 


return [
    '/' => [
        'controller' => App\Controllers\SecurityController::class,
        'method' => 'login',
        'methods' => ['GET', 'POST'],
    ],
    'client/dashboard' => [
        'controller' => App\Controllers\UserController::class,
        'method' => 'index',
        'middlewares' => ['auth'],
        'methods' => ['GET'],
    ],
    'client/{id}' => [
        'controller' => App\Controllers\UserController::class,
        'method' => 'show',
        'middlewares' => ['auth'],
        'methods' => ['GET'],
    ],
];


namespace App\Controllers;

class UserController
{
    public function index()
    {
        echo "Tableau de bord client";
    }

    public function show(array $params)
    {
        echo "Client : " . $params['id'];
    }
}


return [
    'auth' => App\Middlewares\AuthMiddleware::class,
];


namespace App\Middlewares;

class AuthMiddleware
{
    public function __invoke()
    {
        if (!isset($_SESSION['user'])) {
            header('Location: /login');
            exit;
        }
    }
}


namespace App\Controllers;

use App\Repositories\UserRepository;

class DashboardController
{
    private UserRepository $users;

    public function __construct(UserRepository $users)
    {
        $this->users = $users;
    }

    public function index()
    {
        // ...
    }
}


use App\Router\Router;

Router::setDependencyMap([
    App\Contracts\UserRepositoryInterface::class => App\Repositories\UserRepository::class,
]);

[
    'function' => 'nomDeLaFonction',
    'methods' => ['GET'],
]

'/' => 'nomDeLaFonction'

[
    'file' => 'pages/accueil.php',
    'methods' => ['GET'],
]

[
    'controller' => App\Controllers\HomeController::class,
    'method' => 'index',
    'methods' => ['GET'],
]

[
    'function' => 'accueil',
    'middlewares' => ['auth'],
    'methods' => ['GET'],
]
bash
composer 
bash
composer 
text
votre-projet/
├── vendor/
├── controllers/
│   ├── PageController.php
│   └── ClientController.php
├── models/
│   └── ClientModel.php
├── views/
│   ├── accueil.php
│   └── client.php
├── routes.php
├── middlewares.php
├── index.php
└── composer.json
bash
composer dump-autoload
text
votre-projet/
├── vendor/
├── App/
│   ├── Controllers/
│   │   ├── SecurityController.php
│   │   └── UserController.php
│   └── Middlewares/
│       └── AuthMiddleware.php
├── routes.php
├── middlewares.php
├── index.php
└── composer.json