Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/noodlehaus/dispatch
a tiny library for quick and easy PHP apps
https://github.com/noodlehaus/dispatch
framework micro-framework microframework middleware php php-framework php-micro-framework php-microframework php-router router
Last synced: 3 months ago
JSON representation
a tiny library for quick and easy PHP apps
- Host: GitHub
- URL: https://github.com/noodlehaus/dispatch
- Owner: noodlehaus
- Created: 2012-02-01T05:37:06.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2023-05-03T13:36:53.000Z (almost 2 years ago)
- Last Synced: 2024-10-23T16:43:27.119Z (3 months ago)
- Topics: framework, micro-framework, microframework, middleware, php, php-framework, php-micro-framework, php-microframework, php-router, router
- Language: PHP
- Homepage:
- Size: 586 KB
- Stars: 535
- Watchers: 48
- Forks: 103
- Open Issues: 0
-
Metadata Files:
- Readme: README.markdown
Awesome Lists containing this project
README
## dispatch
- a tiny library for quick and easy PHP apps
- requires at least PHP 8.x## functions
Below is the list of functions provided by `dispatch`.
```php
function dispatch(...$args): void;
function route(string $method, string $path, callable ...$handlers): void;
function _404(callable $handler = null): callable;
function apply(...$args): void;
function bind(string $name, callable $transform): void;
function action(string $method, string $path, callable ...$handlers): array;
function response(string $body, int $code = 200, array $headers = []): callable;
function redirect(string $location, int $code = 302): callable;
function serve(array $routes, string $reqmethod, string $reqpath, ...$args): callable;
function phtml(string $path, array $vars = []): string;
function stash(string $key, mixed $value = null): mixed;
```Here's a sample of how you'd usually use them in an app.
```php
response(phtml('not-found'), 404));# Sample route that has a named parameter value. Named parameters gets
# passed to the handlers as the first argument as an associative array.
# Arguments that follow the named parameters array are values passed through
# dispatch(...).
route('GET', '/profiles/:user', function (array $params, $db) {# because of the named param binding for user, this will
# contain the user profile loaded by the named param handler
$user = $params['user'];# the $db argument was forwarded from the dispatch() call below
$meta = loadUserMetadata($db, $user['username']);# phtml() is a function that loads a phtml file and populates it with
# values from the passed in associative array.
return response(phtml(__DIR__.'/templates/profile', ['user' => $user]));
});# Sample route that has no named parameter so it doesn't receive the $params
# associative array. Only dispatch() arguments get forwarded to the handler.
route('GET', '/index', function ($db) {
$users = loadTopUsers($db);
return response(phtml(__DIR__.'/templates/index', ['users' => $users]));
});# Sample route that has an inline middleware passed in. Note that the
# middleware function should still follow the middleware function signature.
route(
'GET',
'/favicon.ico',
# inline middleware
function ($next, $params, $db) {
logDeviceAccess($db, $_SERVER);
return $next();
},
# this is the main handler
function () {
# stash is a request-scoped storage
return response(stash('favicon.ico'));
}
);# App routing entry point. All arguments passed to dispatch get forwarded to
# matching route handlers after the named params array.
$db = createDatabaseConnection();
dispatch($db);```
Once `dispatch(...)` is called, it will try to match the current request to any
of the mapped routes via `route(...)`. When it finds a match, it will then do the
following sequence:1. Execute all named parameter bindings from `bind(...)`
2. Execute all global middleware and matching middleware from `apply(...)`
3. Invoke the handler for the matching route.Because of this sequence, it means that any transformations done by `bind(...)`
mappings will have already updated the values inside the `$params` array that's
forwarded down the execution chain.## license
MIT