webCOMAND

router::route()

The route method interprets the request to find a matching controller to delegate further processing.

If the exit option is TRUE or not set, show_404() will be called if a matching controller is not found.

All options can be specified in a config/routes.php file, or passed in the $options parameter.  Options passed to the method will override those defined in the configuration file.

Prototype

static boolean route(array $options = [])

Parameters

  • options - Optional associative array of options with the following keys.
    • base_dir - The base folder that the controllers and views folders will be under.  Default is NULL, which will assume the same folder as the PHP file the router is called from.
    • base_url - The base URL that must exist at the start of the full URL requested, and that will be removed from the URL before the defined routes, controllers and views are interpreted. Default is '/'.  For example, "/base_url".
    • controllers - The file system path to controller files.  A path starting with a slash is relative to the webCOMAND folder.  Otherwise, the path is relative to the base folder.  The default is controllers/.
    • default - Name of the controller to use if no controller is specified.  If no default controller is specified and the request does not resolve to a controller or static file, a 404 response will be served.
    • exit - If TRUE (default), route_request() will exit() after the controller or show_404() returns.  If FALSE, it will return TRUE if a controller matched the URL, otherwise the last error as a string (show_404() will not be called automatically).
    • files - Path to static files.  "public" is the default.  The path is defined relative to namespace_path if provided, otherwise it is relative to base_dir.  Once defined, the router will match on the defined path relative under the base_dir.  If the router finds a file that matches the request, it will serve the file.  Otherwise it will look for a matching controller.  Set to FALSE to disable serving static files.  If the file is a PHP file, it will be interpretted.
      If the webCOMAND router is first used to route to the MVC router, the webCOMAND router will always check the package's public folder for static files and serve those if they exist, before passing routing to the MVC router.  To avoid that, set this option to something other than "public".
    • namespace - Base PHP namespace of MVC class files.  If not specified, the namespace of the PHP file that calls route() will be used.  NOTE: It is more efficient to specify __NAMESPACE__ rather than having the router determine it as the default.
    • namespace_path - Path where the COMAND auto-loader can find files in the MVC class file namespace (see option above).  If not specified, the path of the PHP file that calls route() will be used.
    • repo - A repo to use, instead of potentially connecting to another repository when controller::repo() is called, which will otherwise automatically connect to the default repository upon first use.
    • request - The portion of the request URL to be interpreted by the router.  If not provided, the router will attempt to determine the portion of the URL that comes after the package routing information.
    • vars - An associative array of variables that can be accessed from controller methods as properties.  For example, 'vars'=>['test'=>123] can be accessed with $this->test.
    • views - The file system path to view files.  A path starting with a slash is relative to the webCOMAND folder.  Otherwise, the path is relative to the base folder.  The default is views/.

Return

If the exit option is TRUE or not set, route_request() will exit() after the controller or show_404() returns and therefore never return.  If the exit option is FALSE, it will return TRUE if a controller matched the URL, otherwise the last error as a string (show_404() will not be called automatically, so that is up to you).

Example

\io_comand_mvc\router::route([
    'exit'=>FALSE
]);