Download the PHP package hypario/router without Composer
On this page you can find all versions of the php package hypario/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hypario/router
More information about hypario/router
Files in hypario/router
Package router
Short Description A little router for beginners in Object Oriented
License MIT
Informations about the package router
A simple Router
This Router is a project for people who want to understand how a router works, and it can be used to start a project really quickly without using any Framework such as Laravel or Symfony
Installation
You can install this package using composer
How to use it
basics
here we will talk about how to use the route for routes that doesn't need parameters
Like every other Router you have to initialize the Router, and write the routes you want like this :
There are other method you can choose such as the POST method
Careful ! those methods don't mean you can reach those pages, now you have to match your URL and the routes
How to match the URL and the route
To match the route you have to use the match method from the router, it will return the route or null if none matched
here $_SERVER['REQUEST_URI']
is used to get the URL, but you can use an object that implement the ServerRequestInterface from the PSR or a custom $_GET that give the URL
When you get the matched route, you can get the handler (here the function).
output : Hello World
if the url is the main page of your website www.mydomain.com
The handler can be a string (like the name of a callable class) or a callable (like here, a function) as the router do not handle the way you're calling the handler.
Routes with parameters
A route with parameters is a classical route, but whenever you want to add parameter,
it must be surrounded by {}
and the name of the parameter and a pattern to match
separated by :
like below.
{name:[a-z]+}
is one needed parameter of the route /hello
, which name
is the name of
the parameter, and [a-z]+
is the pattern to match for the parameter.
Named routes
The name of a route is just one more parameter to the method you want to create
I can now generate an uri to my index which will return /
It also works for routes with parameters, you only need to add the array of parameters to the function
If two routes have the same name, it will generate the path of the LAST defined route