PHP code example of codezone / router

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

    

codezone / router example snippets


use CodeZone\Router;

$router = Router::register($configArray);

$configArray = [
    'container' => $containerInstance // Illuminate\Container\Container
];

$dispatcher = $router->routes(function (Routes $r) {
    $r->get('/my-route', [Plugin\Controllers\MyController::class, 'index']);
    //or
    $r->get('/my-route', 'Plugin\Controllers\MyController@index');
    //or
    $r->get('/my-route', function () {
        return "<h1>Hello World!</h1>";
    });
});

    $r->get('/user/{id}', 'Plugin\Controllers\UserController@show');

namespace CodeZone\Router\Conditions;

class IsFrontendPath implements Condition {
    public function test(): bool {
        return ! is_admin();
    }
}

$r->condition(IsFrontendPath::class, function($r){
    $r->get('/my-route', 'MyController');
});

add_action( Router::class . '\\' . 'conditions', function($conditions) {
    $conditions['isFrontend'] = IsFrontendPath::class;
});

$r->condition('isFrontend', function($r){
    $r->get('/my-route', 'MyController');
});

namespace CodeZone\Router\Middleware;

use CodeZone\Router\Illuminate\Http\Request;
use WP_HTTP_Response;

class LoggedIn implements Middleware {

    public function handle( Request $request, WP_HTTP_Response $response, $next ) {
    
        if ( ! is_user_logged_in() ) {
            $response->set_status( 302 );
            $response->set_data( wp_login_url( $request->getUri() ) );
        }

        return $next( $request, $response );
    }
}

use CodeZone\Router\Middleware\DispatchController;
use CodeZone\Router\Middleware\HandleErrors;
use CodeZone\Router\Middleware\HandleRedirects;
use CodeZone\Router\Middleware\Render;
use CodeZone\Router\Middleware\Route;
use CodeZone\Router\Middleware\SetHeaders;
use CodeZone\Router\Middleware\Stack;

$middleware = [
    Route::class,
    DispatchController::class,
    SetHeaders::class,
    HandleRedirects::class,
    HandleErrors::class,
    Render::class,
];

$stack = container()->makeWith(Stack::class, $middleware);

$stack->run();

$r->get('/my-route', 'MyController')->middleware(Middleware::class);

$r->get('/my-route', 'MyController')->middleware(Middleware::class, function () {
//...
});

$r->get('/my-route', ['MyController', 'myMethod', ['middleware' => Middleware::class]]);
//or
$r->get('/my-route', 'MyController')->middleware([MiddlewareOne::class, MiddlewareTwo::class]);

add_action( Router::class . '\\' . 'middleware', function($middleware) {
    $middleware['auth'] = AuthMiddleware::class;
});

$r->get('/my-route', 'MyController', ['middleware' => 'auth']);

$r->middleware('auth', function($r){
    $r->get('/my-route', 'MyController');
});

$r->get('/my-route', 'MyController', ['middleware' => 'hasCap:manage_options,edit_posts']);

add_filter( Router::class . '\\' . 'response', function($response) {
    return $response;
});

add_filter( Router::class . '\\' . 'error_codes', function($error_codes) {
    unset($error_codes[404]);
    return $error_codes
});

    add_filter(Router::class . '\\' . 'routable_params', function($params) {
        $params['action', 'page', 'tab'];
    });

    // Would match

    $r->get('/contact?action=submit', 'Plugin\Controllers\ContactController@submit');

add_filter(Router::class . '\\' . 'routes', function($routes) {
    $routes->get('/my-route', 'MyController');
return $routes;
});

add_filter(Router::class . '\\' . 'matched_routes', function($matchedRoutes) {
    $matchedRoutes[0] =  true;
    $matchedRoutes[1] =  [
        'handler' => [
           'Plugin\Controllers\MyController'
           'myMethod'
        ],
        'params' => [
            'id' => '3',
        ]
    ]
    return $matchedRoute;
});

add_action( Router::class . '\\' . 'render' ), function($response) {
    echo $response->getContent();
});

add_action(Router::class . '\\' . 'render_json', function($response) {
    echo json_encode($response->getContent());
});

add_action(Router::class . '\\' . 'middleware', function($middleware) {
    $middleware['auth'] = AuthMiddleware::class;
});

add_action(Router::class . '\\' . 'conditions', function($conditions) {
    $conditions['isFrontend'] = IsFrontendPath::class;
});

add_filter(Router::class . '\\' . 'conditions_factory', function(Condition|null $instanceOrNull, array $attributes = []) {
    //Check if this is our named condition
    if ($name !== 'can') {
        return $instanceOrNull;
    }
    
    $className = $attributes['className'] ?? null;
    $name = $attributes['name'] ?? null;
    $signature = $attributes['signature'] ?? null;
    
    //The signature is the part of the route name after the ":". We need to break it into an array.
    $params = explode(',', $signature);
    
    return container->makeWith(HasCap::class, ['params' => $params]);
});

add_filter(Router::class . '\\' . 'conditions_factory', function(Condition|null $instanceOrNull, array $attributes = []) {
    //Check if this is our named condition
    if ($name !== 'can') {
        return $instanceOrNull;
    }
    
    $className = $attributes['className'] ?? null;
    $name = $attributes['name'] ?? null;
    $signature = $attributes['signature'] ?? null;
    
    //The signature is the part of the route name after the ":". We need to break it into an array.
    $params = explode(',', $signature);
    
    return container->makeWith(HasCap::class, ['params' => $params]);
});

add_filter(Router::class . '\\' . 'middleware_factory', function(Middleware|null $instanceOrNull, array $attributes = []) {
    //Check if this is our named middleware
    if ($name !== 'can') {
        return $instanceOrNull;
    }
    
    $className = $attributes['className'] ?? null;
    $name = $attributes['name'] ?? null;
    $signature = $attributes['signature'] ?? null;
    
    //The signature is the part of the route name after the ":". We need to break it into an array.
    $params = explode(',', $signature);
    
    return container->makeWith(UserHasCap::class, ['params' => $params]);
});

add_filter(Router::class . '\\' . 'conditions_factories', function($factories) {
    $factories[UserHasCap::class] = UserHasCapFactory::class;
    return $factories;
});