Siler
Search…
Routing
1
use Siler\Route;
2
3
Route\get('/path', <handler>);
4
Route\post('/path', <handler>);
5
Route\put('/path', <handler>);
6
Route\delete('/path', <handler>);
7
Route\options('/path', <handler>);
Copied!
Also a facade to catch any HTTP method:
1
Route\any('/path', <handler>);
Copied!
Additional and custom HTTP methods can be set using the route function:
1
Route\route('custom', '/path', <handler>);
Copied!
With an array, you can listen for multiple HTTP methods on the same handler:
1
Route\route(['post', 'put'], '/path', <handler>);
Copied!

Route parameters

Route parameters can be defined using Regular Expressions:
1
Route\get('/number/([0-9]+)', <handler>);
Copied!
Or using a little syntax for creating named groups, that is just wrapping the parameter name around curly-brackets:
1
Route\get('/number/{n}', <handler>);
Copied!
The above will match anything, not only numbers. For a fine-grained control, please use Regular Expressions.

Optional parameters

Optional named parameters can be defined using the question mark ? as sufix:
1
Route\get('/hello/{name}?', <handler>);
Copied!
To avoid the need of a trailing slash, add a question mark after it, like regex (because it is regex):
1
Route\get('/hello/?{name}?', <handler>);
Copied!

Route handlers

The <handler> placeholder you saw is where you can put the route logic and it can be contained on the following:

Callables

1
Route\get('/hello/{name}', function (array $routeParams) {
2
echo 'Hello '.($routeParams['name'] ?? 'World');
3
});
Copied!
As a Closures:
1
$handler = function (array $routeParams) {
2
echo 'Hello World';
3
};
4
5
function create_handler() {
6
return function (array $routeParams) {
7
echo 'Hello World';
8
};
9
}
10
11
Route\get('/', $handler);
12
Route\get('/', create_handler());
Copied!
As a method call on array-syntax:
1
class Hello {
2
public function world(array $routeParams) {
3
echo 'Hello World';
4
}
5
}
6
7
$hello = new Hello();
8
Route\get('/', [$hello, 'world']);
Copied!
As a static method call string-syntax:
1
class Hello {
2
static public function world(array $routeParams) {
3
echo 'Hello World';
4
}
5
}
6
7
Route\get('/', 'Hello::world');
Copied!
As any kind of callable:
1
class Hello {
2
public function __invoke(array $routeParams) {
3
echo 'Hello World';
4
}
5
}
6
7
Route\get('/', new Hello());
Copied!

Filenames

Handlers can be a String representing the filename of another PHP file, route parameters will be available at the global $params variable:
index.php
1
Route\get('/hello/{name}', 'pages/home.php');
Copied!
pages/home.php
1
echo 'Hello '.$params['name'];
Copied!

Resources

CRUD routes can be auto-magically be defined for convenience using the Rails and Laravel pattern.
Given this resource declaration:
1
Route\resource('/users', 'api/users');
Copied!
Siler will look for files at path/to/files matching the HTTP URI according to the table below:
HTTP Verb
URI
File
GET
/users
/api/users/index.php
GET
/users/create
/api/users/create.php
POST
/users
/api/users/store.php
GET
/users/{id}
/api/users/show.php
GET
/users/{id}/edit
/api/users/edit.php
PUT
/users/{id}
/api/users/update.php
DELETE
/users/{id}
/api/users/destroy.php
The file structure should look like:
1
index.php
2
/api
3
└── /users
4
├─ index.php
5
├─ create.php
6
├─ store.php
7
├─ show.php
8
├─ edit.php
9
├─ update.php
10
└─ destroy.php
Copied!

Files

You can also let Siler create the routes recursively looking for files at a base path. Then the files names will be used to define the method and the path.
1
Route\files('controllers');
Copied!
Siler will interpret periods (.) as slashes and also maintain folder structure at HTTP path:
Filename
Method
Path
index.get.php
GET
/
index.post.php
POST
/
foo.get.php
GET
/foo
bar/index.get.php
GET
/bar
foo.bar.get.php
GET
/foo/bar
foo/bar.get.php
GET
/foo/bar
foo/bar/index.get.php
GET
/foo/bar
Since { and } are valid chars in a filename, route parameters should work as well, but you can define required parameters prefixing with $ and optional parameters using @:
Filename
Method
Path
foo.{slug}.get.php
GET
/foo/{slug}
foo.$slug.get.php
GET
/foo/{slug}
GET
/foo/{slug?}
Any method is valid, it is guessed based on the penultimate "token":
Filename
Method
Path
foo.options.php
OPTIONS
/foo
foo.x-custom.php
X-CUSTOM
/foo
Note on handlers
When creating routes, be careful about early and lazy evaluations.
1
Route\get('/foo', [new FooController(), 'index']);
Copied!
The example above is early, which means it will call FooController constructor for each request even if it's not a request to /foo.
To make it lazy you can wrap inside a Closure:
1
Route\get('/foo', function () {
2
$controller = new FooController();
3
return $controller->index();
4
});
Copied!
Now FooController is called only when there is a match for route /foo. One downside is that now you have to explicitly manage path parameters, on the other hand is a best practice to do so. It is a good time to validate parameters, convert plain string parameters to meaningful types on your domain or resolve dependencies.
1
Route\get('/users/{id}', function (array $params) use ($ioc) {
2
if (!preg_match('/[0-9]+/', $params['id']) {
3
$controller = $ioc->resolve(ErrorController::class);
4
return $controller->invalid('IDs must be numbers');
5
}
6
7
$controller = $ioc->resolve(UsersController::class);
8
return $controller->show($params['id']);
9
});
Copied!

Request

Body

You can grab the raw request body using:
1
use Siler\Http\Request;
2
3
$body = Request\raw();
Copied!
Parse as URL-encoded data using:
1
$params = Request\params();
Copied!
Parse as JSON using:
1
$resource = Request\json();
Copied!

$_GET and $_POST superglobals

Or get data from a Form and from the Query String using:
1
$input = Request\post('input');
2
$searchTerm = Request\get('q');
Copied!
Calling them without arguments will tell Siler to return all the values as an array:
1
$data = Request\post();
2
$queryString = Request\get();
Copied!
You can also pass a default value if the key isn't present in the GET or POST super-globals:
1
$input = Request\post('input', 'default-value');
Copied!

Headers

Also conveniently get a header as easy as for the body:
1
$contentType = Request\header('Content-Type');
Copied!
e.g. Serving both JSON and Form requests:
1
$data = Request\header('Content-Type') == 'json' ? Request\json() : Request\post();
Copied!

Response

Siler also have convenient functions to simplify HTTP responses.
You can output JSON encoded body with proper headers in just one line:
1
use Siler\Http\Response;
2
3
Response\json(['error' => false, 'message' => 'It works']);
Copied!
It will already output the given data, you don't need to call echo or print so use carefully, it's not a encoder, it's an output-er.

Headers

Same easy as for Request, you can set HTTP response headers with the header function at Response namespace:
1
Response\header('Access-Control-Allow-Origin', 'https://know-domain.tld');
2
Response\header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
Copied!
Last modified 2yr ago