YaRouter is (yet another) PHP router. It's built to be simple and easy to use with minimal configuration.
It does one thing only. It routs URI/URL requests (Such as:
https://example.com/foo) to a view/template (Such as:
/path/to/view/foo.html). Keeping your URLs clean and friendly!
composer require digi-brains/ya-router 1.1.2
YaRouter assumes 2 things.
You are, through your server or document root config file, directing all traffic through a single entry point/bootstrap file; such as
Your views file-names reflect your URL structure. For example, If your URL is
example.com/foo/bar/my-page, YaRouter expects to find the view for this page at
/views | |__index.html (example.com)<-default view | |__/about | | | |_team.html (example.com/about/team.html) | | | |_products.html (example.com/about/products.html) | |__/services | |_service1.html (example.com/services/service1.html) | |_service2.html (example.com/services/service2.html) | |_service3.html (example.com/services/service3.html) ...etc...
Set up your index.php file (or whatever file you are pointing your inbound requests to).
If you are using an autoloader, like composer
require-ing the autoloader script:
require __DIR__ . '/vendor/autoload.php';.
use the YaRouter\Router.
If you're not using an autoloader
include the Router.php script
Next define your parameters:
$viewsThis is the path to the main directory where your views are stored.
$typeThis is the type of view file you are calling (extension only). E.g., html, php, etc...
$defaultThe name of the default view file (without the extension). Typically used for home or index page.
Finally, create a
new Router() object with your parameters and then call the
Kitchen Sink Example:
(index.php) <?php require __DIR__ . '/vendor/autoload.php'; use YaRouter\Router; $views = 'path/to/views'; $type = 'html'; $default = 'index'; $r = new Router( $views, $type, $default ); $r->get_view();