velsym/routing
PHP routing package.
Last updated
PHP routing package.
Last updated
Abstract class which gives methods that help to reduce boilerplate code.
render(output)
- sets the response body.
Returns:
Params:
output
- string containing data meant to be rendered.
redirectToPath(path)
- redirects user to specified path.
Returns:
Params:
path
- string of desired path e.g."/home/popular"
.
redirectToRoute(routeName)
- redirects user to the route based on its name.
Returns:
Params:
routeName
- name of the route that user should be redirected to.
Router is responsible for executing specific piece of code according to the given criteria.
__construct(absoluteRoutesPath, routesNamespace)
- creates configured router.
Returns:
Router
Params:
absoluteRoutesPath
- absolute path containing all route files.
routesNamespace
- namespace in which routes work. Namespace must be defined in composer's autoloading.
handle()
- starts the process of routing. Should be called after initial configuration.
Returns:
void
dumpRoutes()
- var_dumps registered routes. Helpful for debugging.
Returns:
void
Used to define class's method as route.
path
- url of the route.
Defines piece of code that should be executed before the route's code is executed.
params
- a key-value array that is passed as arguments to the constructor of the middleware defined in middleware
parameter.
Example above is giving an example of how middleware can be used. Here it's used to check if the user is authenticated. If so, then it means that the user has a profile and can access his profile settings. Otherwise he's not logged in and cannot access the settings since the profile does not exist. This is one of many applications in which middleware can be used.
methods
- an array of methods that are allowed for the route.
The route attribute configured above tells the that if path /
is requested and method is only GET
. Only then the router should execute code given in home
method in MainRoutes class.
middleware
- fully qualified class name of the middleware. Middleware extends .