PHP code example of solophp / router

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

    

solophp / router example snippets


use Solo\Router\RouteCollector;

$router = new RouteCollector();

// Invokable controller
$router->get('/users', UserController::class);

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

// Named routes
$router->get('/users/{id}', [UserController::class, 'show'])->name('users.show');

// Optional parameters
$router->get('/posts[/{page}]', [PostController::class, 'index']);

// With middleware and page attribute
$router->post('/admin/posts', [PostController::class, 'store'], 
    [AuthMiddleware::class], 
    'admin.posts'
);

// Route groups
$router->group('/admin', function(RouteCollector $router) {
    $router->get('/dashboard', [AdminController::class, 'dashboard']);
    $router->get('/users', [AdminController::class, 'users']);
}, [AuthMiddleware::class]);

// Match route
$route = $router->matchRoute('GET', '/users/123');
if ($route) {
    // Handle the route
    $handler = $route['handler'];
    $args = $route['args']; // ['id' => '123']
    $middleware = $route['middleware'];
    $page = $route['page']; // Optional page identifier
}

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

// Optional parameters
$router->get('/posts[/{category}[/{page}]]', [PostController::class, 'index']);

// Parameters with regex patterns
$router->get('/users/{id:[0-9]+}', [UserController::class, 'show']);
$router->get('/articles/{slug:[a-z0-9-]+}', [ArticleController::class, 'show']);

$router->get('/users', UserController::class);
$router->post('/users', UserCreateController::class);

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

$router->group('/admin', function(RouteCollector $router) {
    $router->get('/dashboard', [AdminController::class, 'dashboard'], [], 'admin.dashboard');
    $router->get('/settings', [AdminController::class, 'settings'], [], 'admin.settings');
}, [AdminMiddleware::class]);

// Simple page attribute
$router->get('/about', [PageController::class, 'about'], [], 'about');

// With middleware and page
$router->get('/profile', [ProfileController::class, 'show'], 
    [AuthMiddleware::class], 
    'user.profile'
);

// In groups
$router->group('/blog', function(RouteCollector $router) {
    $router->get('/', [BlogController::class, 'index'], [], 'blog.index');
    $router->get('/{slug}', [BlogController::class, 'show'], [], 'blog.show');
});

// Single middleware
$router->get('/profile', [ProfileController::class, 'show'], [AuthMiddleware::class]);

// Multiple middleware
$router->get('/admin/settings', [SettingsController::class, 'show'], [
    AuthMiddleware::class,
    AdminMiddleware::class
]);

// Group middleware
$router->group('/api', function(RouteCollector $router) {
    $router->get('/users', [ApiController::class, 'users']);
}, [ApiAuthMiddleware::class]);