sidvind / php-routes
Routing for PHP projects
Installs: 3 783
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 0
Open Issues: 2
Requires
- php: ^8.1 || ^8.2
Requires (Dev)
- php-coveralls/php-coveralls: 2.5.3
- phpunit/phpunit: 9.6.5
- squizlabs/php_codesniffer: 3.7.2
This package is auto-updated.
Last update: 2025-02-09 13:57:20 UTC
README
Routing for MVC-ish PHP projects.
composer require sidvind/php-routes
Example
Put routes in a separate file, e.g. routes.php
:
<?php /* basic routes */ $get('foo', ['to' => 'MyController#foo']); $post('bar/:id/baz', ['to' => 'MyController#update']); /* use :var for variables */ /* automatically setup RESTful routes */ $resource('article', [], function($r){ $r->members(function($r){ $r->patch('frobnicate'); /* maps to PATCH /article/:id/frobnicate */ }); $r->collection(function($r){ $r->patch('twiddle'); /* maps to PATCH /article/twiddle */ }); }); /* scoping */ $scope(':lang', [], function($r){ $r->get('barney'); /* maps to GET /:lang/barney */ });
Create a dispatcher:
<?php class Dispatcher extends Sidvind\PHPRoutes\Router { public function dispatch($url, $method){ if ( $match = $this->match($url, $method) ){ $class = "{$match->controller}Controller"; $controller = new $class(); return call_user_func_array([$controller, $match->action], $match->args); } else { /* 404 */ } } } $router = new Dispatcher('routes.php'); $router->dispatch($url, $method);
To preview/debug routes use bin/php-routes
:
# bin/php-routes routes.php
GET /foo MyController#foo #^/foo(?P<format>\.\w+)?$#
POST /bar/:id/baz MyController#update #^/bar/(?P<id>[A-Za-z0-9\-_\.]+)/baz(?P<format>\.\w+)?$#
article GET /article Article#list #^/article(?P<format>\.\w+)?$#
...
# bin/php-routes routes.php get /foo
Controller: MyController
Action: foo
Format:
Arguments:
[]
# bin/php-routes routes.php get /bar
bin/php-routes: url doesn't match any route.