An open API service indexing awesome lists of open source software.

https://github.com/morphable/micro

new routing lib
https://github.com/morphable/micro

Last synced: 2 months ago
JSON representation

new routing lib

Awesome Lists containing this project

README

          

# Micro
a micro framework based on the psr-15 and psr-7 standard

### Simple usage
```php
routing();

$router->add('GET', '/user/:id', function (ServerRequestInterface $request, array $args) {
$id = $args['id'];

/** @link https://www.php-fig.org/psr/psr-7/ */
return $response;
});

try {
$response = $micro->handle($request); // \Psr\Http\Message\ResponseInterface
} catch (\Exception $e) {
// 404
}
```

### Callbacks
```php
add('GET', '/user/:id', ['controller', 'method']);

// __invoke
$router->add('GET', '/user/:id', 'controller');

// static method
$router->add('GET', '/user/:id', [controller::class, 'method']);

function callback(ServerRequestInterface $request, array $args) {
return $response;
}

// function
$router->add('GET', '/user/:id', 'callback');

// annonymous function
$router->add('GET', '/user/:id', function (ServerRequestInterface $request, array $args) {
return $response;
});
```

### Route pattern
```php
add('GET', '/user/:userId/profile', function ($request, $args){
$args['userId'] // second parameter in url
});

$router->add('GET', '/callback/?:channel', function ($request, $args) {
$args['channel'] // second parameter or null
})
```

### Middleware
```php
handle($request);
}
}

$router->add('GET', '/user/:id', ['controller', 'method'])
->middleware('middleware'); // from container
```

### Groups
```php
group('api', function ($router) { // prefix of api

$router->add('GET', '/', ['controller', 'method']); // pattern: /api

$router->group('user', function ($router) {
$router->add('GET', '/:id', ['controller', 'method']); // pattern: /api/user/:id
})->middleware('middleware');

})->middleware('middleware'); // counts for every route inside

```

### After call
```php
add('GET', '/', ['controller', 'method'])
->after(function ($reqest, $args, $response): void {})
->after(['class', 'method'])
->after('function');
```