From df185e75d4cbaf278d8dbffa9b46365bbd9887d9 Mon Sep 17 00:00:00 2001
From: Guilherme Nascimento Teeny for PHPTeeny is a micro-route system that is really micro, supports PHP 5.3 to PHP 8, is extremely simple and ready to use.
For create your project use:
-composer create-project inphinit/teeny <project name> +composer create-project inphinit/teeny <project name>Replace
-<project name>
by your project name, for exemple, if want create your project with “blog” name (folder name), use:composer create-project inphinit/teeny blog +composer create-project inphinit/teeny blogDownload without composer
@@ -1123,49 +1131,49 @@API
Add and remove routes
For create a new route in
-index.php
put like this:$app->action('GET', '/myroute', function () { +$app->action('GET', '/myroute', function () { echo 'Test!'; });You can use
-return
:$app->action('GET', '/myroute', function () { +$app->action('GET', '/myroute', function () { return 'Test!'; });For remove a route use
-null
value, like this:$app->action('GET', '/myroute', null); +$app->action('GET', '/myroute', null);
Route include file
For include a file uses like this:
-$app->action('GET', '/myroute', 'foo/bar/test.php'); +$app->action('GET', '/myroute', 'foo/bar/test.php');
If
-foo/bar/test.php
not found in project will display the following error:Warning: require(foo/bar/test.php): failed to open stream: No such file or directory in /home/user/blog/vendor/teeny.php on line 156 +Warning: require(foo/bar/test.php): failed to open stream: No such file or directory in /home/user/blog/vendor/teeny.php on line 156 Fatal error: require(): Failed opening required 'foo/bar/test.php' (include_path='.') /home/user/blog/vendor/teeny.php on line 156HTTP status
For retrieve HTTP status from SAPI (Apache, Ngnix, IIS) or previously defined in the script itself use like this:
-$var = $app->status(); +$var = $app->status();
For retrieve into a route use like this:
-$app->action('GET', '/myroute', function () use ($app) { +$app->action('GET', '/myroute', function () use ($app) { echo 'HTTP status: ', $app->status(); });For set a new HTTP status use like this (eg.: emit 404 Not Found):
-$app->status(404); +$app->status(404);
For set into route use like this (a example with condition/if):
-$app->action('GET', '/report', function () use ($app) { +$app->action('GET', '/report', function () use ($app) { $file = 'data/foo.csv'; if (is_file($file)) { @@ -1181,20 +1189,20 @@HTTP statusNamed params in route
You can use params like this:
-$app->action('GET', '/user/<user>', function ($params) { +$app->action('GET', '/user/<user>', function ($params) { var_dump($params); });If access a URL like this
-http://mywebsite/user/mary
returns:array(2) { +array(2) { ["user"]=> string(3) "mary" }Another example:
-$app->action('GET', '/article/<name>-<id>', function ($params) use ($app) { +$app->action('GET', '/article/<name>-<id>', function ($params) use ($app) { // Only ID numerics are valids if (ctype_digit($params['id'])) { echo 'Article ID: ', $params['id'], '<br>'; @@ -1208,13 +1216,13 @@Named params in route
Article ID: mary +Article ID: mary Article name: 1000Types of params named in routes
An example, only numeric id are valids:
-$app->action('GET', '/article/<name>-<id:num>', function ($params) { +$app->action('GET', '/article/<name>-<id:num>', function ($params) { echo 'Article ID: ', $params['id'], '<br>'; echo 'Article name: ', $params['name']; }); @@ -1272,7 +1280,7 @@Types of params named in routes
For add new patterns use like this
-Teeny::setPattern()
, examples:$app->setPattern('example', '[A-Z]\d+'); +$app->setPattern('example', '[A-Z]\d+'); $app->action('GET', '/custom/<myexample:example>', function ($params) use ($app) { echo '<h1>custom pattern</h1>'; @@ -1284,4 +1292,4 @@Types of params named in routesAnd for access this route exemple use
http://mysite/test/A00001
orhttp://mysite/test/C02
, start with upper-case letter and after width a integer number
-If you need more features you can experience the Inphinit PHP framework: https://inphinit.github.io