Added arrow function example and description

This commit is contained in:
SteamPixel 2021-02-08 10:52:14 +01:00
parent ae1c66b0f7
commit b3e284ea15
2 changed files with 22 additions and 1 deletions

View File

@ -57,6 +57,25 @@ Route::run('/api/v1');
Do not forget to edit the basepath in .htaccess too if you are on Apache2. Do not forget to edit the basepath in .htaccess too if you are on Apache2.
## Use return instead of echo
You don't have to use `echo` to output your content. You can also use the `return` statement. Everything that gets returned is echoed automatically.
```php
// Add your first route
Route::add('/', function() {
return 'Welcome :-)';
});
```
## Use arrow functions
Since PHP 7.4 you can also use arrow functions to output your content. So you can easily use variables from outside and you can write shorter code.
Please be aware that an Arrow function must always return a value. Therefore you cannot use `echo` here.
You can find an example in index.php. However, this is deactivated, as it only works from PHP 7.4.
```php
Route::add('/arrow/([a-z-0-9-]*)', fn($foo) => 'This is a working arrow function example. Parameter: '.$foo );
```
## Enable case sensitive routes, trailing slashes and multi match mode ## Enable case sensitive routes, trailing slashes and multi match mode
The second, third and fourth parameters of `Route::run('/', false, false, false);` are set to false by default. The second, third and fourth parameters of `Route::run('/', false, false, false);` are set to false by default.
Using this parameters you can switch on and off several options: Using this parameters you can switch on and off several options:

View File

@ -29,6 +29,7 @@ function navi() {
<li><a href="'.BASEPATH.'phpinfo">PHP Info</a></li> <li><a href="'.BASEPATH.'phpinfo">PHP Info</a></li>
<li><a href="'.BASEPATH.'äöü">Non english route: german</a></li> <li><a href="'.BASEPATH.'äöü">Non english route: german</a></li>
<li><a href="'.BASEPATH.'الرقص-العربي">Non english route: arabic</a></li> <li><a href="'.BASEPATH.'الرقص-العربي">Non english route: arabic</a></li>
<li><a href="'.BASEPATH.'arrow/test123">Arrow function test (please enable this route first)</a></li>
<li><a href="'.BASEPATH.'aTrailingSlashDoesNotMatter">aTrailingSlashDoesNotMatter</a></li> <li><a href="'.BASEPATH.'aTrailingSlashDoesNotMatter">aTrailingSlashDoesNotMatter</a></li>
<li><a href="'.BASEPATH.'aTrailingSlashDoesNotMatter/">aTrailingSlashDoesNotMatter/</a></li> <li><a href="'.BASEPATH.'aTrailingSlashDoesNotMatter/">aTrailingSlashDoesNotMatter/</a></li>
<li><a href="'.BASEPATH.'theCaseDoesNotMatter">theCaseDoesNotMatter</a></li> <li><a href="'.BASEPATH.'theCaseDoesNotMatter">theCaseDoesNotMatter</a></li>
@ -143,7 +144,8 @@ Route::add('/return', function() {
// Arrow function example // Arrow function example
// Note: You can use this example only if you are on PHP 7.4 or higher // Note: You can use this example only if you are on PHP 7.4 or higher
// Route::add('/arrow/([a-z-0-9-]*)', fn($foo) => 'This is a working arrow function example. Parameter: '.$foo ); // $bar = 'bar';
// Route::add('/arrow/([a-z-0-9-]*)', fn($foo) => navi().'This is a working arrow function example. <br/> Parameter: '.$foo. ' <br/> Variable from global scope: '.$bar );
// Trailing slash example // Trailing slash example
Route::add('/aTrailingSlashDoesNotMatter', function() { Route::add('/aTrailingSlashDoesNotMatter', function() {