1. Go to this page and download the library: Download neuron-php/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/ */
neuron-php / routing example snippets
use Neuron\Routing\Attributes\Get;
class HomeController
{
#[Get('/')]
public function index()
{
return 'Hello World';
}
}
use Neuron\Routing\Attributes\Get;
use Neuron\Routing\Attributes\Post;
use Neuron\Routing\Attributes\Put;
use Neuron\Routing\Attributes\Delete;
class UsersController
{
#[Get('/users')]
public function index() { }
#[Get('/users/:id')]
public function show(int $id) { }
#[Post('/users')]
public function store() { }
#[Put('/users/:id')]
public function update(int $id) { }
#[Delete('/users/:id')]
public function destroy(int $id) { }
}
#[Get('/admin/users', name: 'admin.users.index', filters: ['auth'])]
public function index() { }
#[Post('/admin/users', name: 'admin.users.store', filters: ['auth', 'csrf'])]
public function store() { }
use Neuron\Routing\Attributes\RouteGroup;
use Neuron\Routing\Attributes\Get;
use Neuron\Routing\Attributes\Post;
#[RouteGroup(prefix: '/admin', filters: ['auth'])]
class AdminController
{
#[Get('/dashboard')] // Becomes /admin/dashboard with 'auth' filter
public function dashboard() { }
#[Post('/users', filters: ['csrf'])] // Becomes /admin/users with ['auth', 'csrf'] filters
public function createUser() { }
}
#[Get('/api/v1/users')]
#[Get('/api/v2/users')]
public function getUsers()
{
// Handle both API versions
}
class Home
{
#[Get('/', name: 'home')]
public function index() { }
}
// app/Controllers/Landing.php
class Landing extends Controller
{
#[Get('/custom/landing', name: 'landing')]
public function index()
{
return $this->renderHtml(OK, [], 'custom-home');
}
}
// ❌ ERROR: Duplicate route
#[Get('/users')]
public function index() { }
#[Get('/users')] // Same method + path
public function list() { }
// ❌ ERROR: Duplicate name
#[Get('/users', name: 'users')]
public function index() { }
#[Post('/users/create', name: 'users')] // Same name
public function store() { }
// ✅ ALLOWED: Same path, different methods
#[Get('/users', name: 'users.index')]
public function index() { }
#[Post('/users', name: 'users.store')]
public function store() { }
// ✅ ALLOWED: Multiple routes to same handler
#[Get('/user/:id')]
#[Get('/profile/:id')]
#[Get('/member/:id')]
public function show($id)
{
// Backward compatibility or URL aliasing
}
$router = Router::instance();
$router->setStrictMode(false); // Allow duplicates (first match wins)
use Neuron\Routing\Router;
use Neuron\Routing\Filters\RateLimitFilter;
use Neuron\Routing\RateLimit\RateLimitConfig;
$router = Router::instance();
// Create rate limit configuration
$config = new RateLimitConfig([
'enabled' => true,
'storage' => 'redis', // Options: redis, file, memory (testing only)
'requests' => 100, // Max requests per window
'window' => 3600 // Time window in seconds (1 hour)
]);
// Create and register the filter
$rateLimitFilter = new RateLimitFilter($config);
$router->registerFilter('rate_limit', $rateLimitFilter);
// Apply globally to all routes
$router->addFilter('rate_limit');
// Or apply to specific routes
$router->get('/api/data', $handler, 'rate_limit');