WebFioriAPI ReferenceLearnDownloadContributeLearn{{result.parent_page}} > {{result.title}}{{result.title}}Classes{{result.class_name}}{{result.summary}}Methods{{result.name}}{{result.summary}}

namespace \webfiori\framework\router

class Router

Class Attributes Summary

const API_ROUTEA constant that represents API route.const CLOSURE_ROUTEA constant that represents closure route.const CUSTOMIZEDA constant for custom directory route.const VIEW_ROUTEA constant that represents view route.

Class Methods Summary

public static function addRoute(array$options)Adds new route to a file inside the root folder.public static function api(array$options)Adds new route to a web services set.public static function base()Returns the base URI which is used to create routes.public static function closure(array$options)Adds new closure route.public static function getBase()Returns the value of the base URI which is appended to the path.public static function getRouteUri()Returns an object of type 'RouterUri' which contains route information.public static function getUriObj(string$path)Returns an object of type 'RouterUri' that represents route URI.public static function getUriObjByURL(string$url)Returns an object of type 'RouterUri' which contains URL route information.public static function getVarValue(string$varName)Returns the value of a variable which exist in the path part of the URI.public static function hasRoute(string$path)Checks if a given path has a route or not.public static function incSiteMapRoute()Adds a route to a basic xml site map.public static function notFound()Call the closure which was set if a route is not found.public static function page(array$options)Adds new route to a web page.public static function printRoutes()Display all routes details.public static function redirect(string$path, string$to, int$code)Adds a redirect route.public static function removeAll()Remove all routes which has been added to the array of routes.public static function removeRoute(string$path)Removes a route given its path.public static function route(string$uri)Redirect a URI to its route.public static function routes()Returns an associative array of all available routes.public static function routesAsRouterUri()Returns an associative array that contains all routes.public static function routesCount()Returns the number of routes at which the router has.public static function setOnNotFound(callable$func)Sets a callback to call in case a given rout is not found.public static function uriObj(RouterUri$routerUri)Adds an object of type 'RouterUri' as new route.

Class Attributes Details

const API_ROUTEA constant that represents API route. It is simply the root directory where APIs should be created.const CLOSURE_ROUTEA constant that represents closure route. The value of the constant is 'func'.const CUSTOMIZEDA constant for custom directory route. const VIEW_ROUTEA constant that represents view route. It is simply the root directory where web pages should be created.

Class Methods Details

public static function addRoute(array$options)Adds new route to a file inside the root folder.

Parameters:

  • array $options An associative array of options. Available options are:
    • path: The path part of the URI. For example, if the requested URI is 'http://www.example.com/user/ibrahim', the path part of the URI is '/user/ibrahim'. It is possible to include variables in the path. To include a variable in the path, its name must be enclosed between {}. The value of the variable will be stored in either the array $_GET or $_POST after the requested URI is resolved. If we use the same example above to get any user profile, We would add the following as a path: 'user/{username}'. In this case, username will be available in $_GET['username']. Note that its possible to get the value of the variable using the method Router::getVarValue()
    • route-to: The path to the file that the route will point to. It can be any file in the scope of the variable ROOT_DIR.
    • as-api: If this parameter is set to true, the route will be treated as if it was an API route. This means that the constant 'API_ROUTE' will be initiated when a request is made to the route. Note that if the PHP file that the route is pointing to represents an API, no need to add this option. Default is false.
    • case-sensitive: Make the URL case sensitive or not. If this one is set to false, then if a request is made to the URL 'https://example.com/one/two', It will be the same as requesting the URL 'https://example.com/OnE/tWO'. Default is true.
    • languages: An indexed array that contains the languages at which the resource can have. Each language is represented as two characters such as 'AR'.
    • vars-values: An optional associative array which contains sub indexed arrays that contains possible values for URI vars. This one is used when building the sitemap.
    • middleware: This can be a name of a middleware to assign to the route. This also can be a middleware group. In addition to that, this can be an array that holds middleware names or middleware groups names.
    • in-sitemap: If set to true, the given route will be added to the basic site map which can be generated by the router class.
    • methods: An optional array that can have a set of allowed request methods for fetching the resource. This can be also a single string such as 'GET' or 'POST'.
    • action: If the class that the route is pointing to represents a controller, this index can have the value of the action that will be performed (the name of the class method).
    • routes This option is used to have sub-routes in the same path. This option is used to group multiple routes which share initial part of a path.
Returns: booleanThe method will return true if the route was created. If a route for the given path was already created, the method will return false.
public static function api(array$options)Adds new route to a web services set. Note that the route which created using this method will be added to 'global' and 'api' middleware groups.

Parameters:

  • array $options An associative array that contains route options. Available options are:
    • path: The path part of the URI. For example, if the requested URI is 'http://www.example.com/user/ibrahim', the path part of the URI is '/user/ibrahim'. It is possible to include variables in the path. To include a variable in the path, its name must be enclosed between {}. The value of the variable will be stored in either the array $_GET or $_POST after the requested URI is resolved. If we use the same example above to get any user profile, We would add the following as a path: 'user/{username}'. In this case, username will be available in $_GET['username']. Note that its possible to get the value of the variable using the method Router::getVarValue(). Note that for any route that points to a web services set, a variable which has the name 'service-name' must be added.
    • route-to: The path to the API file. The root folder for all APIs is '/apis'. If the API name is 'get-user-profile.php', then the value of this parameter must be '/get-user-profile.php'. If the API is in a sub-directory inside the APIs directory, then the name of the directory must be included.
    • case-sensitive: Make the URL case sensitive or not. If this one is set to false, then if a request is made to the URL 'https://example.com/one/two', It will be the same as requesting the URL 'https://example.com/OnE/tWO'. Default is true.
    • vars-values: An optional associative array which contains sub indexed arrays that contains possible values for URI vars. This one is used when building the sitemap.
    • middleware: This can be a name of a middleware to assign to the route. This also can be a middleware group. In addition to that, this can be an array that holds middleware names or middleware groups names.
    • methods: An optional array that can have a set of allowed request methods for fetching the resource. This can be also a single string such as 'GET' or 'POST'.
    • routes This option is used to have sub-routes in the same path. This option is used to group multiple routes which share initial part of a path.
Returns: booleanThe method will return true if the route was created. If a route for the given path was already created, the method will return false.
public static function base()Returns the base URI which is used to create routes. Returns: stringThe base URL which is used to create routes. The returned value is based on one of two values. Either the value that is returned by the method 'Util::getBaseURL()' or the method 'SiteConfig::getBaseURL()'.public static function closure(array$options)Adds new closure route. Note that the route which created using this method will be added to 'global' and 'closure' middleware groups.

Parameters:

  • array $options An associative array that contains route options. Available options are:
    • path: The path part of the URI. For example, if the requested URI is 'http://www.example.com/user/ibrahim', the path part of the URI is '/user/ibrahim'. It is possible to include variables in the path. To include a variable in the path, its name must be enclosed between {}. The value of the variable will be stored in either the array $_GET or $_POST after the requested URI is resolved. If we use the same example above to get any user profile, We would add the following as a path: 'user/{username}'. In this case, username will be available in $_GET['username']. Note that its possible to get the value of the variable using the method Router::getVarValue()
    • route-to: A closure (A PHP function).
    • closure-params: An array that contains values which can be passed to the closure.
    • as-api: If this parameter is set to true, the route will be treated as if it was an API route. This means that the constant 'API_ROUTE' will be initiated when a request is made to the route. Default is false.
    • case-sensitive: Make the URL case sensitive or not. If this one is set to false, then if a request is made to the URL 'https://example.com/one/two', It will be the same as requesting the URL 'https://example.com/OnE/tWO'. Default is true.
    • languages: An indexed array that contains the languages at which the resource can have. Each language is represented as two characters such as 'AR'.
    • vars-values: An optional associative array which contains sub indexed arrays that contains possible values for URI vars. This one is used when building the sitemap.
    • middleware: This can be a name of a middleware to assign to the route. This also can be a middleware group. In addition to that, this can be an array that holds middleware names or middleware groups names.
    • in-sitemap: If set to true, the given route will be added to the basic site map which can be generated by the router class.
    • methods: An optional array that can have a set of allowed request methods for fetching the resource. This can be also a single string such as 'GET' or 'POST'.
    • routes This option is used to have sub-routes in the same path. This option is used to group multiple routes which share initial part of a path.
Returns: booleanThe method will return true if the route was created. If a route for the given path was already created, the method will return false. Also, if 'route-to' is not a function, the method will return false.
public static function getBase()
Returns the value of the base URI which is appended to the path. This method is similar to calling the method Router::base().
Returns: string
public static function getRouteUri()Returns an object of type 'RouterUri' which contains route information. When the method Router::route() is called and a route is found, an object of type 'RouterUri' is created which has route information.Returns: RouterUri|nullAn object which has route information. If the method 'Router::route()' is not yet called or no route was found, the method will return null.public static function getUriObj(string$path)Returns an object of type 'RouterUri' that represents route URI.

Parameters:

  • string $path The path part of the URI.
Returns: RouterUri|nullIf a route was found which has the given path, an object of type 'RouterUri' is returned. If no route is found, null is returned.
public static function getUriObjByURL(string$url)Returns an object of type 'RouterUri' which contains URL route information.

Parameters:

  • string $url A string that represents a URL (such as 'https://example.com/my-resource').
Returns: RouterUri|nullIf a resource was found which has the given route, an object of type RouterUri is returned. Other than that, null is returned. Note that if the URI is invalid, the method will return null. Also, if the library 'http' is not loaded, the method will return null.
public static function getVarValue(string$varName)Returns the value of a variable which exist in the path part of the URI.

Parameters:

  • string $varName The name of the variable. Note that it must not include braces.
Returns: string|nullThe method will return the value of the variable if it was set. If it is not set or routing is still not yet happend, the method will return null.
public static function hasRoute(string$path)Checks if a given path has a route or not.

Parameters:

  • string $path The path which will be checked (such as '/path1/path2')
Returns: booleanThe method will return true if the given path has a route.
public static function incSiteMapRoute()Adds a route to a basic xml site map. If this method is called, a route in the form 'http://example.com/sitemam.xml' and in the form 'http://example.com/sitemam' will be created. The method will check all created routes objects and check if they should be included in the site map. Note that if a URI has variables, it will be not included unless possible values are given for the variable.public static function notFound()Call the closure which was set if a route is not found. public static function page(array$options)Adds new route to a web page. Note that the route which created using this method will be added to 'global' and 'web' middleware groups.

Parameters:

  • array $options An associative array that contains route options. Available options are:
    • path: The path part of the URI. For example, if the requested URI is 'http://www.example.com/user/ibrahim', the path part of the URI is '/user/ibrahim'. It is possible to include variables in the path. To include a variable in the path, its name must be enclosed between {}. The value of the variable will be stored in either the array $_GET or $_POST after the requested URI is resolved. If we use the same example above to get any user profile, We would add the following as a path: 'user/{username}'. In this case, username will be available in $_GET['username'].
    • route-to: The path to the view file. The root folder for all views is '/pages'. If the view name is 'view-user.php', then the value of this parameter must be '/view-user.php'. If the view is in a sub-directory inside the views directory, then the name of the directory must be included.
    • case-sensitive: Make the URL case sensitive or not. If this one is set to false, then if a request is made to the URL 'https://example.com/one/two', It will be the same as requesting the URL 'https://example.com/OnE/tWO'. Default is true.
    • languages: An indexed array that contains the languages at which the resource can have. Each language is represented as two characters such as 'AR'.
    • vars-values: An optional associative array which contains sub indexed arrays that contains possible values for URI vars. This one is used when building the sitemap.
    • middleware: This can be a name of a middleware to assign to the route. This also can be a middleware group. In addition to that, this can be an array that holds middleware names or middleware groups names.
    • in-sitemap: If set to true, the given route will be added to the basic site map which can be generated by the router class.
    • methods: An optional array that can have a set of allowed request methods for fetching the resource. This can be also a single string such as 'GET' or 'POST'.
    • routes This option is used to have sub-routes in the same path. This option is used to group multiple routes which share initial part of a path.
Returns: booleanThe method will return true if the route was created. If a route for the given path was already created, the method will return false.
public static function printRoutes()Display all routes details. public static function redirect(string$path, string$to, int$code)Adds a redirect route.

Parameters:

  • string $path The path at which when the user visits will be redirected.
  • string $to A path or a URL at which the user will be sent to.
  • int $code HTTP redirect code. Can have one of the following values: 301, 302, 303, 307 and 308. Default is 301 (Permanent redirect).
public static function removeAll()Remove all routes which has been added to the array of routes. public static function removeRoute(string$path)Removes a route given its path.

Parameters:

  • string $path The path part of route URI.
Returns: booleanIf the route is removed, the method will return true. If not, The method will return false.
public static function route(string$uri)Redirect a URI to its route.

Parameters:

public static function routes()Returns an associative array of all available routes. Returns: arrayAn associative array of all available routes. The keys will be requested URIs and the values are the routes.public static function routesAsRouterUri()Returns an associative array that contains all routes. The returned array will have two indices, 'static' and 'variable'. The 'static' index will contain routes to resources at which they don't contain variables in their path part. Each index of the two will have another sub associative array. The indices of each sub array ill be URLs that represents the route and the value at each index will be an object of type 'RouterUri'.Returns: arrayAn associative array that contains all routes.public static function routesCount()Returns the number of routes at which the router has. Returns: intNumber of routes.public static function setOnNotFound(callable$func)Sets a callback to call in case a given rout is not found.

Parameters:

  • callable $func The function which will be called if the rout is not found.
public static function uriObj(RouterUri$routerUri)Adds an object of type 'RouterUri' as new route.

Parameters:

  • RouterUri $routerUri An object of type 'RouterUri'.
Returns: booleanIf the object is added as new route, the method will return true. If the given parameter is not an instance of 'RouterUri' or a route is already added, The method will return false.
mdi-menuAll Classesmdi-chevron-leftAnchorBrCodeSnippetHTMLDocHTMLListHTMLNodeHTMLTableHeadNodeInputJsCodeLabelListItemOrderedListParagraphRadioGroupTableCellTableRowUnorderedListInvalidNodeNameExceptionTemplateNotFoundExceptionCaseConverterJsonJsonConverterJsonIJsonTypesPropertyAPIFilterAbstractWebServiceManagerInfoServiceParamTypesRequestRequestParameterResponseUriWebServicesManagerAccessAutoLoaderConfigConfigControllerDBEAbstractWebServiceExtendedWebServicesManagerFileLoggerPrivilegePrivilegesGroupThemeThemeLoaderUploadFileUploaderUserUtilWebFioriAppLanguageErrorBoxHTTPCodeViewMessageBoxServerErrViewStarterPageWebPageDatabaseSessionStorageDefaultSessionStorageMSSQLSessionDataTableMSSQLSessionsTableMySQLSessionDataTableMySQLSessionsTableSessionSessionOperationsSessionStorageSessionsManagerRouterRouterUriAbstractMiddlewareMiddlewareManagerEmailMessageSMTPAccountSMTPServerClassLoaderExceptionFileExceptionInitializationExceptionInvalidCRONExprExceptionMissingLangExceptionNoSuchThemeExceptionRoutingExceptionSMTPExceptionSessionExceptionUIExceptionAbstractJobCronCronEmailCronJobJobArgumentCronLoginViewCronTasksViewCronViewCronLoginServiceCronLogoutServiceCronServicesManagerForceCronExecutionServiceGetJobsServiceCLICLICommandInputStreamOutputStreamStdInStdOutClassWriterLangClassWriterQueryClassWriterServiceHolderThemeClassWriterWebServiceWriterCreateCLIClassHelperCreateCronJobCreateMiddlewareCreateTableCreateTableObjCreateThemeHelperCreateWebServiceAddCommandCreateCommandCronCommandHelpCommandListCronCommandListRoutesCommandListThemesCommandRunSQLQueryCommandSettingsCommandTestRouteCommandUpdateSettingsCommandUpdateTableCommandVersionCommandAbstractQueryColumnColumnFactoryConditionConnectionConnectionInfoDatabaseDatabaseExceptionDateTimeValidatorEntityMapperExpressionForeignKeyJoinTableResultSetSelectExpressionTableWhereExpressionMySQLColumnMySQLConnectionMySQLQueryMySQLTableMSSQLColumnMSSQLConnectionMSSQLQueryMSSQLTableAbstractCollectionComparableLinkedListNodeQueueStack
Powered By: WebFiori Framework, Vue and VuetifyAll Rights Reserved © 2018 - 2024