SlaxWeb Framework
 All Classes Namespaces Functions Variables Pages
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
SlaxWeb\Router\Dispatcher Class Reference

Public Member Functions

 __construct (\SlaxWeb\Router\Container $routes,\SlaxWeb\Hooks\Container $hooks,\Psr\Log\LoggerInterface $logger)
 
 dispatch (Request $request, Response $response)
 
 enableSegMatch (string $namespace, array $params=[], string $prepend="", string $defaultMethod="index")
 

Public Attributes

 uriPrepend
 

Protected Member Functions

 findRoute (int $method, string $uri)
 
 checkContainer (int $method, string $uri)
 
 handleNoMatch ()
 
 dispatchRoute (Route $route, array $params)
 
 dispatchController (string $uri)
 
 posix2Pcre (string $regex, array $names=["params","named"])
 
 addParams (array $matches)
 

Protected Attributes

 $routes = null
 
 $hooks = null
 
 $logger = null
 
 $addQueryParams = []
 
 $segBasedMatch
 

Constructor & Destructor Documentation

SlaxWeb\Router\Dispatcher::__construct ( \SlaxWeb\Router\Container  $routes,
\SlaxWeb\Hooks\Container  $hooks,
\Psr\Log\LoggerInterface  $logger 
)

Class constructor

Set retrieved Routes Container, Hooks Container, and the Logger to the internal properties.

Parameters
\SlaxWeb\Router\Container$routesRoutes container
\SlaxWeb\Hooks\Container$hooksHooks container
\Psr\Log\LoggerInterface$loggerLogger implementing PSR3

Member Function Documentation

SlaxWeb\Router\Dispatcher::addParams ( array  $matches)
protected

Add additional parameters

Prepares the found matches from the URI and injects them into the 'addQueryParams' property.

Parameters
array$matchesRegex matches
Returns
void
SlaxWeb\Router\Dispatcher::checkContainer ( int  $method,
string  $uri 
)
protected

Check Routes Container

Iterates the routes container and tries to match the request to a Route in the container. Returns the matching Route object if found, or null otherwise.

Parameters
int$methodRequest Method
string$uriRequest Uri
Returns
|null
SlaxWeb\Router\Dispatcher::dispatch ( Request  $request,
Response  $response 
)

Dispatch Request

Dispatch the Request to the propper Route. Tries to find a matching Route for the retrieved Request object, and calls that Routes action callable along with Response, and any other input parameters as arguments for the action. If Segment based URI matching is enabled, the dispatcher will try to route the incoming request to a matched controller and method.

Parameters
\SlaxWeb\Router\Request$requestRequest object
\SlaxWeb\Router\Reponse$responseResponse object
mixed$unknownAny further parameter is sent to Route action
Returns
void
SlaxWeb\Router\Dispatcher::dispatchController ( string  $uri)
protected

Dispatch request to controller

Try and match the Request URI with an existing controller and its method. If such match is found, dispatch the request to said controller and method, and return bool(true) upon successful execution. If a match is not found bool(false) is returned.

Parameters
string$uriRequest URI string
Returns
bool
SlaxWeb\Router\Dispatcher::dispatchRoute ( Route  $route,
array  $params 
)
protected

Dispatch route

Dispatch the route by executing its action.

Parameters
\SlaxWeb\Router\Route$routeRoute object
array$paramsArray of parameters for the Route action
Returns
void
SlaxWeb\Router\Dispatcher::enableSegMatch ( string  $namespace,
array  $params = [],
string  $prepend = "",
string  $defaultMethod = "index" 
)

Enable segment Based URI Matching

Enables the segment based URI matching, sets the Controller namespace, and the default method to call if the second segment is not found in the URI. Default method has the default value of string("index").

Parameters
string$namespaceController namespace
array$paramsController constructor parameters
string$prependURI prepend for segment based URI matching
string$defaultMethodDefault controller method for single segment URIs
Returns
SlaxWeb\Router\Dispatcher::findRoute ( int  $method,
string  $uri 
)
protected

Find matching Route

Try and obtain the route that bests fits the request and return it. If no such route is found, and no 404 route exists, nor is one returned from a 'routeNotFound' hook execution, null is returned.

Parameters
int$methodRequest Method
string$uriRequest Uri
Returns
|null
SlaxWeb\Router\Dispatcher::handleNoMatch ( )
protected

Handle No Matching Route Found

Execute the Route Not Found Hook definition with the help of the Hook component and return a valid Route object if it is found in the Hook execution return data. If no valid route is returned by the hook, try to obtain the 404 route from the Route Container object and return it.

Returns
|null
SlaxWeb\Router\Dispatcher::posix2Pcre ( string  $regex,
array  $names = ["params",
"named"]   
)
protected

POSIX named class to PCRE capturing group

Replace the special POSIX named classes with normal named capturing groups.

Parameters
string$regexRaw regexp string
array$namesPOSIX class names array, default: ["params", "named"]
Returns
string Replaced regexp string

Member Data Documentation

SlaxWeb\Router\Dispatcher::$segBasedMatch
protected
Initial value:
= [
"enabled" => false
SlaxWeb\Router\Dispatcher::uriPrepend
Initial value:
=> "",
"controller" => [
"namespace" => "",
"defaultMethod" => "",
"params" => []
]
]

The documentation for this class was generated from the following file: