PHP code example of gcworld / routing

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

    

gcworld / routing example snippets




namespace App\Handler;

use GCWorld\Routing\Abstracts\AdvancedHandler;
use GCWorld\Routing\Attributes\Route;

#[Route(
    name: 'users_show',
    patterns: ['/users/:number'],
    title: 'User details',
)]
final class ShowUser extends AdvancedHandler
{
    public function get(): string
    {
        $userId = $this->args[0] ?? null;

        return 'User ' . $userId;
    }
}

use GCWorld\Routing\LoadRoutes;

LoadRoutes::getInstance()
    ->addPath(__DIR__ . '/src/Handler')
    ->generateRoutes(force: true);

use GCWorld\Routing\Core\CoreRouter;

$router = CoreRouter::getInstance();
$router->forward();

use GCWorld\Routing\Abstracts\AdvancedHandler;
use GCWorld\Routing\Attributes\Route;
use GCWorld\Routing\Attributes\RouteMeta;
use GCWorld\Routing\Attributes\RoutePexCheck;
use GCWorld\Routing\Enumerations\RoutePexCheckType;

#[Route(
    autoWrapper: true,
    session: true,
    name: 'reports_show',
    patterns: ['/reports/:number', '/report/:number'],
    title: 'Report',
    meta: [
        new RouteMeta('section', 'reports'),
    ],
    preArgs: ['tenant'],
    postArgs: ['html'],
    pex: [
        new RoutePexCheck(RoutePexCheckType::STANDARD, 'reports.[1].view'),
    ],
)]
final class ShowReport extends AdvancedHandler
{
    // ...
}

/**
 * @router-pattern /reports/:number
 * @router-name reports_show
 * @router-session true
 * @router-autoWrapper true
 * @router-title Report
 * @router-meta section:reports
 * @router-preArgs tenant
 * @router-postArgs html
 * @router-pexCheck reports.[1].view
 */
final class ShowReport
{
    // ...
}

$router = CoreRouter::getInstance();

$url = $router->reverse('users_show', [42]);
// /users/42

$router->setUserClassName(\App\Security\CurrentUser::class);
$router->forward();

$router->setPageWrapperName(\App\View\PageWrapper::class);

$router->addHook('404', \App\Routing\ErrorHandler::class . '::notFound');
$router->addHook('403_pex', \App\Routing\ErrorHandler::class . '::denied');

$router->attachRedisCache($redis);
console
./dc exec php composer lint
./dc exec php composer phpstan
./dc exec php composer phpcs
./dc exec php composer test