crodas / dispatcher
Dispatcher generator
Requires
- php: >=5.5
- crodas/file-util: ^0.2
- crodas/notoj: >=1.3.2
- crodas/simple-view-engine: ^0.1
- crodas/watch-files: ~0.1
- symfony/http-foundation: ^2.6
Requires (Dev)
- crodas/cli: ^0.1.0
- dev-master
- v1.1.1
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.1.23
- v0.1.22
- v0.1.21
- v0.1.20
- v0.1.19
- v0.1.18
- v0.1.17
- v0.1.16
- v0.1.15
- v0.1.14
- v0.1.13.1
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- dev-develop
- dev-feature/http-foundation
- dev-feature/better-api
- dev-cache-filter
This package is auto-updated.
Last update: 2024-12-12 19:27:53 UTC
README
Dispatcher
is a routing library that maps URLs to actions.
Installing
You can install using composer:
composer require crodas/dispatcher:"^1.0"
Defining routes
Routes are defined using annotations for methods or functions.
use Symfony\Component\HttpFoundation\Request; /** * A single function can have multiple routes. * * @Route("/{user}") * @Route("/user/{user}") */ function handler(Request $req, $user) { return "Hello {$user}"; } /** * You can also set default values and use multiple routes * * @Route("/foo/{bar}") * @Route("/foo", {bar="index"}) */ function do_something_with_bar(Request $request, $bar) { print "I've got bar=" . $bar; } /** * If @Route is define at a class it would behave as a * namespace of prefix for the routes defined at their * methods * * @Route("/admin") */ class Foobar { /** * This handles POST /admin/login * * @Route("/login") * @Method POST */ public function do_login(Request $req) { } /** * This handles GET /admin/login * * @Route("/login") */ public function login(Request $req) { } }
Dispatcher
walks over the filesystem looking for @Route
annotations on functions and methods.
How to use it
$router = new Dispatcher\Router; $router-> // where our controllers are located ->addDirectory(__DIR__ . "/../projects"); // Do the router $router->doRoute();
By default, it runs in production mode
and it won't rebuild the routes on changes. If you wish to run on development mode, you should do this:
$router->development();
Filters
To simply things, Dispatcher
doesn't allow you yet to define regular expressions to validate your placeholders. Instead it lets you to define functions which validates and modified the values of your placeholders.
/** * In this URL, the placeholder user have a Filter, that means * if the controller is called we can be sure we get a valid * user object. * * @Route("/profile/{user}") */ function show_profile(Request $req, $user) { return "Hi {$user->name}"; } /** * Validate {user} placeholders * * Check if the user exists in the database, if it does exists * it will return true and the controller will be called. * * @Filter("user") */ function some_filter(Request $req, $name, $value) { $userobj = DB::getUserById($value); if ($userobj) { /* I'm overriding the placeholder $name for an object of the Database */ $req->attributes->set($name, $userobj); return true; } return false; }
The filter should return a false if $value
is not valid the router will jump to the next rule or will throw a notfound
exception. $name
is used because one callback can be used as multiple filters.
Expensive Filters
can be cached with @Cache <ttl>
annotation. The cache mechanism is defined in the application with a class which implements FilterCache
interface.
Valid Patterns
/foo/bar
/foo/{user}
/foo/{user:user1}/{user:user2}
: Inside theRequest
object the user objects will be nameduser1
anduser2
to avoid name collisions./foo/{user:u1}-vs-{user:u2}.{ext}
: We can have multiple variables inside one single directory level as long as they are separated by constants.