1. Go to this page and download the library: Download jetfirephp/routing library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// Require composer autoloader
tance
$collection = new \JetFire\Routing\RouteCollection();
// Define your routes
// ...
// Create an instance of Router
$router = new \JetFire\Routing\Router($collection)
// select your matcher
$matcher1 = new \JetFire\Routing\Matcher\ArrayMatcher($router);
$matcher2 = new \JetFire\Routing\Matcher\UriMatcher($router);
// set your matcher to the router
$router->setMatcher([$matcher1,$matcher2])
// Run it!
$router->run();
$options = [
'view_dir' => '_VIEW_DIR_PATH_',
'ctrl_namespace' => '_CONTROLLERS_NAMESPACE_'
];
$collection = new \JetFire\Routing\RouteCollection(null,$options);
// or
$collection = new \JetFire\Routing\RouteCollection();
$collection->setOption($options);
// or
$collection = new \JetFire\Routing\RouteCollection();
$collection->addRoutes(null,$options)
return [
// controller and template resolver
// call first the controller and render then the template
// if the template is not found, the controller is returned
'/home/log' => [
'use' => 'HomeController@log',
'template' => 'Home/log.php', //in your controller you can return an array of data that you can access in your template
],
// dynamic route with arguments
'/home/user-:id-:slug' => [
'use' => 'HomeController@page',
'template' => 'Home/log.php',
'arguments' => ['id' => '[0-9]+','slug' => '[a-z-]*'],
],
];
return [
// suppose we have the following methods in the AccountController :
// public function create();
// public function read($id);
// public function update($id);
// public function destroy($id);
// if the uri is /account/create the router will call the associated method in the controller
'/account/*' => [
'use' => 'AccountController@{method}',
],
];
return [
// closure and template matching
// call first the closure and render then the template
'/home/log' => [
'use' => function(){
return ['name' => 'Peter'];
}
'template' => 'Home/log.php', // in log.php you can access the return data like this : $name ='Peter'
],
'/home/user-:id-:slug' => [
'use' => function($id,$slug){
return ['id' => $id,'slug' => $slug];
},
'template' => 'Home/log.php',
'arguments' => ['id' => '[0-9]+','slug' => '[a-z-]*'],
],
];
// Create RouteCollection instance
$collection = new \JetFire\Routing\RouteCollection();
// Block routes
$collection->addRoutes('admin_routes_path',['view_dir' => 'admin_view_path' , 'ctrl_namespace' => 'admin_controllers_namespace','prefix' => 'admin']);
$collection->addRoutes('public_routes_path',['view_dir' => 'public_view_path' , 'ctrl_namespace' => 'public_controllers_namespace']);
// Create an instance of Router
$router = new \JetFire\Routing\Router($collection)
// Select your matcher
$router->addRouter(new \JetFire\Routing\Matcher\ArrayMatcher($router));
// Run it!
$router->run();
$router->setConfig([
// You can add/remove extension for views
// default extension for views
'templateExtension' => ['.html', '.php', '.json', '.xml'],
// If you use template engine library, you can use this to render the view
// See the 'Integration with other libraries' section for more details
'templateCallback' => [],
// If you want to add a dependency injection container for your controllers constructor or method
// for example if your controller 'HomeController' method 'log' method
$options = [
// your view directory
'view_dir' => 'view_directory',
// your controllers namespace
'ctrl_namespace' => 'controllers_namespace',
// your routes prefix
'prefix' => 'your_prefix'
];
$collection->addRoutes('routes_file_1',['prefix' => 'prefix_1']); // all routes in routes_file_1 begin with prefix_1/
$collection->addRoutes('routes_file_2',['prefix' => 'prefix_2']); // all routes in routes_file_2 begin with prefix_2/
// Your middleware file
return [
// global middleware are called every time
'global_middleware' => [
// Here you define your middleware class to be called
'app\Middleware\Global',
],
// block middleware are called when the current route block match one of the following block
'block_middleware' => [
// You define here for each block the middleware class to be called
'/app/Blocks/PublicBlock/' => 'app\Middleware\Public',
'/app/Blocks/AdminBlock/' => 'app\Middleware\Admin',
'/app/Blocks/UserBlock/' => 'app\Middleware\User',
],
// class middleware are called when the controller router match one of the following controller
'class_middleware' => [
// You define here for each controller the middleware class to be called
'/app/Blocks/PublicBlock/Controllers/HomeController' => 'app\Middleware\Home',
],
// route middleware are called when the current route match one of the following middleware name
'route_middleware' => [
// You define here a name to the middleware and assign the class to be called
// You have to specify this name to the route like this : `'middleware' => 'home'`
'home' => 'app\Middleware\App'
],
];
namespace app\Middleware;
class Global{
// Middleware class must implements handle method
// object passed in argument will be inject automatically
public function handle(){
// here you put your code
// ...
}
}
class MyCustomMatcher implements MatcherInterface{
public function __construct(Router $router);
// in this method you can check if the current uri match your expectation
// return true or false
// if it match you have to set your route target with an array of params and the dispatcher class name to be called
// $this->setTarget(['dispatcher' => '\My\Custom\Dispatcher\Class\Name', 'other_params' => 'values']);
public function match();
// set your route target $this->router->route->setTarget($target);
public function setTarget($target = []);
// set your resolver
public function setResolver($resolver = []);
// you can add multiple resolver method in the same matcher
public function addResolver($resolver);
// to retrieve your resolver
public function getResolver();
// dispatcher yo be called
public function setDispatcher($dispatcher = []);
public function addDispatcher($dispatcher);
}
class MyCustomDispatcher implements DispatcherInterface{
public function __construct(Router $router);
// your target to call
// you can get your route target information with $this->route->getTarget()
public function call();
}
$router->addMatcher('MyCustomMatcher');
class MyCustomMatcher extends ArrayMatcher implements MatcherInterface{
public function __construct(Router $router){
parent::__construct($router);
// your custom match method
$this->addResolver('customResolver');
}
public function customResolver(){
// your code here
// ...
// then you set the route target with the dispatcher
}
}
class MyCustomDispatcher implements DispatcherInterface{
public function __construct(Router $router);
// your target to call
// you can get your route target information with $this->route->getTarget()
public function call();
}
// Twig template engine
er();
// Other template engine
$tpl = new \Acme\Template\Template();
$router->setConfig([
'templateCallback' => [
// if the router find a template with twig enxtension then it will call the twig template engine
'twig' => function($route){
$loader = new Twig_Loader_Filesystem($route->getTarget('block'));
$twig = new Twig_Environment($loader, []);
$template = $twig->loadTemplate($route->getTarget('template'));
echo $template->render($route->getData());
},
// for other template engine
'tpl' => function($route) use ($tpl){
$tpl->load($route->getTarget('template'));
$tpl->setData($route->getData());
$tpl->display();
}
],
// Other configuration
// ...
]);
return [
'{subdomain}.{host}/home' => [
'use' => 'AdminController@index',
'name' => 'admin.home.index',
'subdomain' => 'admin' // could be a regex for multiple subdomain
]
];
// JetFire\Routing\RouteCollection
$collection->
routesByName // routes url by their name
countRoutes // count routes block
addRoutes($collection,$options = []) // set your routes
getRoutes($key = null) // return all routes
getRoutePath($name,$params = []) // return the url of route
setPrefix($prefix) // $prefix can be a string (applied for every collection)
// or an array (for each collection you can specify a prefix)
setOption($options = []) // set your routes option
generateRoutesPath() // generate routes url by their name
// JetFire\Routing\Router
$router->
response // JetFire\Routing\ResponseInterface instance
route // JetFire\Routing\Route instance
collection // JetFire\Routing\RouteCollection instance
middlewareCollection // middleware collection
matcher // list of matcher
dispatcher // the dispatcher instance
setConfig($config) // router configuration
getConfig() // get router configuration
run() // run the router with the request url
// JetFire\Routing\Route
$route->
set($args = []) // set your route array parameters
getUrl() // return the route url
setUrl($url) // set the route url
getName() // return the route name
setName($name) // set the route name
getCallback() // return the route callback (template,controller or anonymous function)
setCallback($callback) // set the route callback
getMethod() // return the route method (GET,POST,PUT,DELETE)
getDetail() // return the route detail
setDetail($detail) // set the route detail
addDetail($key,$value) // add a detail for the route
getTarget($key = null) // return the route target (dispatcher,template or controller|action or function)
setTarget($target = []) // set the route target
hasTarget($key = null) // check if the route has the following target
getData() // return data for the route
__call($name,$arguments) // magic call to get or set route detail
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.