The sFire\Routing\Router can be used to redirect your users to other URL's and retrieve more information about the current request.
It can be handy to retrieve the route for retrieving information about the current route. For example, you can view the route identifier or module name. The getRoute will return a sFire\Routing\Extend\Route with information.
$route = Router :: getRoute();
//Some example methods
$route -> getIdentifier(); //Returns the identifier
$route -> getModule(); //Returns the module name
$route -> getAction(); //Returns the action name
The getUrl method will return a sFire\Utils\URLParser instance (see HTTP URL parser). This method is a handy tool to get information about the requested URL.
$url = Router :: getUrl();
print_r($url);
//Output similar to:
sFire\Utils\URLParser Object
(
[url:sFire\Utils\URLParser:private] => stdClass Object
(
[scheme] => https
[host] => example.com
[path] => /customer/details
[query] => id=5
)
)
You can redirect internal to another controller using the redirect method.
For example purpuse, we have in our routes.php the following routes:
Router :: get('customer/overview', 'customer.overview') -> module('App') -> controller('Article') -> action('overview') -> domain(['example\.com', 'sub\.example\.com']);
Router :: get('customer/details/{int}', 'customer.details') -> module('App') -> controller('Article') -> action('details') -> domain(['example\.com', 'sub\.example\.com']);
You can redirect internal to these routes by calling the redirect method. You may chain the methods and execute the redirect by calling one of the following:
Redirecting to the customer overview. Because we have defined that only the get method is accepted, we should use the same method for redirection.
Router :: redirect('customer.overview') -> get();
Redirecting to the customer overview. This will execute the redirect with the current method like GET or POST. If the currentMethod is not set, than the "get" method is used by default.
Router :: redirect('customer.overview') -> currentMethod();
To redirect to the customer details, we need to give some parameters to the route. We can do this by calling the params method.
Router :: redirect('customer.details') -> params([52]) -> get();
You can also give a new domain name by calling the domain method.
Router :: redirect('customer.overview') -> domain('sub.example.com') -> get();
You can chain all the methods.
Router :: redirect('customer.details') -> params([52]) -> domain('sub.example.com') -> get();
You can us the execute method to redirect the browser to a specific route identifier. This method accepts one parameter for overwriting the HTTP status code.
Router :: forward('customer/overview') -> execute(302); //Adding a header "302 Found"
For example purpuse, we have in our routes.php the following routes:
Router :: get('customer/overview', 'customer.overview') -> module('App') -> controller('Article') -> action('overview') -> domain(['example\.com', 'sub\.example\.com']);
Router :: get('customer/details/{int}', 'customer.details') -> module('App') -> controller('Article') -> action('details') -> domain(['example\.com', 'sub\.example\.com']);
Simulation of a Location header.
//Redrect browser to "http://example.com/customer/overview"
Router :: forward('customer.overview') -> execute();
Simulation of a refresh header. Send the browser to the new url after 2 seconds.
//Redrect browser to "http://example.com/customer/overview" after 2 seconds
Router :: forward('customer.overview') -> seconds(2) -> execute();
To redirect to the customer details, we need to give some parameters to the route. We can do this by calling the params method.
//Redrect browser to "http://example.com/customer/details/52"
Router :: forward('customer.details') -> params([52]) -> execute();
You can also give a new domain name by calling the domain method.
//Redrect browser to "http://sub.example.com/customer/overview"
Router :: forward('customer.overview') -> domain('sub.example.com') -> execute();
The new domain name will have the same HTTP protocol as the current request. You can overwrite this by setting the seconds parameter.
Router :: forward('customer.overview') -> domain('sub.example.com', 'https') -> execute();
This will forward the browser to "https://sub.example.com/customer/overview".
You can chain all the methods.
Router :: forward('customer.details') -> params([52]) -> domain('sub.example.com') -> seconds(2) -> execute();
It is sometimes necessary to retrieve the link behind a URL identifier defined in the routes.php. The url method will convert a URL identifier to a link:
In routes.php:
Router :: get('article/{int}', 'article.view') -> module('App') -> controller('Article') -> action('view');
In the controller:
Router :: url('article.view', [52]); //Outputs "article/52"
This can also be used in the template engine.
If you want to check if a route exists in the routes.php, you may use the routeExists method. This method accepts a String parameter as the unique identifier for the route and returns a Boolean true if exists and Boolean false if not. You can also set the second parameter for checking if a route exists in combination with a domain name which is set in routes.php with the domain method.
if(true === Router :: routeExists('article.view')) {
//Route exists
}
if(true === Router :: routeExists('article.view', 'example.com')) {
//Route exists for givin domain
}
If you are using domains in routes.php, you can check if an domain exists. This method is not valuable if you are using it in the module itself (for example a controller), but is quite handy if you use this method in a Schedule to know if an domain has been set.
This method accepts a String parameter as the domain name you want to check and returns a Boolean true if exists and Boolean false if not.
if(true === Router :: domainExists('example.com')) {
//Domain exists
}