Standard Route Mapping
Note: these docs are for the Standard Router implementation, provided with FW Api. If you are using a custom router implementation, this will likely not apply.
The Route Mapper is responsible for defining which server-side url calls will call which methods on which controllers. In the default FW Kit blueprint, this class is found in server/src/Router.php
and extends the default FW\Router\Standard\AbstractRouteMapper
class.
// Router.php
namespace SomeApp;
class Router extends \FW\Router\Standard\AbstractRouteMapper {
// ...
}
Note: if you wish to define a custom class location for your route mapper, you can do so by specifying a router
key in your config. However, that value must be a valid classname and extend the AbstractRouteMapper.
There are several different methods that you can define/override in this class.
- The
load
method
The load
method is used to retrieve the controller object instance. It takes one argument, the fully qualified classname of the controller. The method returns the object instance of the controller, with all of the various properties set.
- The
router
variable
The route mapper has access to the Slim router instance, as an instance variable called $router
. This is what you use to actually map the routes to the controller functions. See the slim mapping documentation for more information.
- The
setup
function
Before the actual mapRoutes function is called, a setup function can be defined that sets up any needed dependencies before you map the routes. This is currently rarely, if ever used, but it is still there if there are specific dependencies that need to be set up before calling mapRoutes. In the past this function was used to set up the Utils
class (calling an init()
function), but now that the Utils
class has been updated to the RoutingUtils
class trait, it is no longer required in this context.
- The
mapRoutes
method
The mapRoutes
method is the only required method that you must implement in your route mapper. This is where you should map all of your routes to the actions in your controllers. You can do this manually, through the basic methods provided by the slim router, or as is recommended, you can employ a couple of the utility functions that are provided.