velsym/routing
PHP routing package.
Velsym\Routing
RouteHelper
Abstract class which gives methods that help to reduce boilerplate code.
Methods
render(output)
- sets the response body.Returns:
Response
Params:
output
- string containing data meant to be rendered.
redirectToPath(path)
- redirects user to specified path.Returns:
Response
Params:
path
- string of desired path e.g."/home/popular"
.
redirectToRoute(routeName)
- redirects user to the route based on its name.Returns:
Response
Params:
routeName
- name of the route that user should be redirected to.
Router
Router is responsible for executing specific piece of code according to the given criteria.
Methods
__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
Velsym\Routing\Attributes
Route
Used to define class's method as route.
Params
path
- url of the route.methods
- an array of HTTP methods that are allowed for the route.
Examples
The route attribute configured above tells the Router that if path /
is requested and HTTP method is only GET
. Only then the router should execute code given in home
method in MainRoutes class.
Middleware
Defines piece of code that should be executed before the route's code is executed.
Params
middleware
- fully qualified class name of the middleware. Middleware extends BaseMiddleware.params
- a key-value array that is passed as arguments to the constructor of the middleware defined inmiddleware
parameter.
Examples
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.
Last updated