Skip to content

Commit

Permalink
Added example for injecting variables to local scope
Browse files Browse the repository at this point in the history
  • Loading branch information
SteamPixel committed Feb 8, 2021
1 parent f245e21 commit d7775a9
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function navi() {
<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: arabic</a></li>
<li><a href="'.BASEPATH.'global/test123">Inject variables to local scope</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>
Expand Down Expand Up @@ -135,8 +136,21 @@ function navi() {
echo 'Arabic example. Non english letters should work too <br>';
});

// Use variables from global scope
// You can use for example use() to inject variables to local scope
// You can use global to register the variable in local scope
$foo = 'foo';
$bar = 'bar';
Route::add('/global/([a-z-0-9-]*)', function($param) use($foo) {
global $bar;
navi();
echo 'The param is '.$param.'<br/>';
echo 'Foo is '.$foo.'<br/>';
echo 'Bar is '.$bar.'<br/>';
});

// Return example
// Returned data gets echoed
// Returned data gets printed
Route::add('/return', function() {
navi();
return 'This text gets returned by the add method';
Expand Down

0 comments on commit d7775a9

Please sign in to comment.