Download the PHP package louiss0/slim-route-registry without Composer
On this page you can find all versions of the php package louiss0/slim-route-registry. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download louiss0/slim-route-registry
More information about louiss0/slim-route-registry
Files in louiss0/slim-route-registry
Package slim-route-registry
Short Description This package allows you to register routes and create resource controllers using attributes php
License MIT
Homepage https://github.com/louiss0/slim-route-registry
Informations about the package slim-route-registry
aliases: [slim-route-registry-library] tags: [slim, route-registry] note type: Main
Slim Route Registry
Usage
The slim route registry library is a library that uses controllers and the metadata that comes from them to automatically create route groups and setup middleware. The controller is used as the model for how routes are created and what methods they use. This library is created for slim php only.
Installation
Setup
To setup the library you have to first create the slim app and pass it into the setup method.
Doing this will setup all of the systems necessary to create controllers called "Resource Controllers"
Sections
- Resources
- Manual Resources
- Automatic Resources
- Group Scoping
- Middleware
- Resource Middleware
- Method Middleware
- Group Middleware
Resources
A Resource Controller is a controller that either has a route method attribute attached to a method or uses a Automatic Registration Method. It's a controller with the necessary information needed for the Resource method to work. To create a resource you create a class then attach a Route Method Attribute to one of its methods.
-
To register a resource you use the Route Registry resource method.
- To register multiple resources use the resources method
Manual Resources
A Manual Resource is a controller that is created by attaching route method attributes as to its methods. The route attributes will tell the RouteMethodAttribute
is the main attribute all other attributes stem inherit from this attribute with a predefined http request method.
A route method attribute is an attribute that takes three parameters.
- path - The path of the route relative to the group scope
- name - The name of the route that will be created
- method - The http request in lowercase the method will respond to
Note you don't need to use the Route Method Attribute directly you can use Attributes that are inherited from them instead.
Automatic Resources
An Automatic Resource is a controller that uses a Automatic Registration Method.
An Automatic Registration Method is a method that has a name that will be used by the resource method to register a route based on it's name.
Note: If you add a route method attribute to one of these methods you'll get an error
Note: The id parameter must be an int
Group Scoping
In most apps you want to have the power to wrap resources under a group that will be be used to either call middleware before any of their methods are called or controlling the path the user must use to access a resource. To do this you use the group method.
- The path is the pattern that will be used in the group method
- The closure will be the function that will be called in the closure of the group method. When you use the resource method in the closure passed the resource path will be appended to the group path as usual
Middleware
You can use middleware in your app by using Use Middleware Attributes on methods and controllers and controller methods. You can tell the resource method to create the same middleware to be called before a request handler is called or before multiple request handlers are called.
Resource Middleware
To apply middleware to a route group created from the resource method you use the Use Middleware Attribute on a controller.
Method Middleware
When it comes to putting middleware in a route you must use a class that implements the Middleware interface as a attribute or the following attributes on a controller:
-
The method names are the names of the handlers for the routes the middleware will be added to. The middleware will only go to those routes.
- The method names are the names of the handlers for the routes the middleware will not be added to. The middleware go to every route but those routes.
Note: The middleware will be applied in the following order.
UseMiddlewareOn
UseMiddlewareExceptFor
Note: The
UseMiddlewareOn
and UseUseMiddlewareExceptFor
attributes are repeatable
Group Middleware
To add middleware to a group use the created by the RouteRegistry::group()
method use