Menu

Library Integration
Integration with other libraries

Overview

Library Integration

Integrate other libraries with Leaf's Core router by making good use of the use keyword to pass dependencies into the handling functions.

$tpl = new \Acme\Template\Template(); $leaf->get('/', function() use ($tpl) { $tpl->load('home.tpl'); $tpl->setdata(array( 'name' => 'Leaf' )); }); $leaf->run(function() use ($tpl) { $tpl->display(); });

Disabling subfolder support

In case you don't want Leaf's Core router to automatically adapt itself to the folder its being placed in, it's possible to manually override the basePath by calling setBasePath(). This is necessary in the (uncommon) situation where your entry script and your entry URLs are not tightly coupled (e.g. when the entry script is placed into a subfolder that does not need be part of the URLs it responds to)..

// Override auto base path detection $leaf->setBasePath('/'); $leaf->get('/', function() { echo 'Index'; }); $leaf->get('/hello', function() { echo 'Hello!'; }); $leaf->run();

If you were to place this file into a subfolder (e.g. public_html/some/sub/folder/index.php), it will still mount the routes onto the domain root (e.g. /) and thus respond to https://www.example.org/ and https://www.example.org/hello (given that your .htaccess file – placed at the document root level – rewrites requests to it)

After Router Middleware / Run Callback

Run one (1) middleware function, name the After Router Middleware (in other projects sometimes referred to as after app middlewares) after the routing was processed. Just pass it along the $leaf->run() function. The run callback is route independent.

$leaf->run(function() { … });
Note: If the route handling function has exit()ed the run callback won't be run.

Next Steps

Response
Leaf Dates
Named Parameters
Sub-routing