sFire PHP Framework

Router

The sFire\Routing\Router can be used to redirect your users to other URL's and retrieve more information about the current request.

In this section we will handle:

  • Retrieving the current route
  • Retrieving the current URL
  • Internal redirect
  • Forwarding browser
  • Converting URL identifiers to links
  • Check if a route exists

Retrieving the current route

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

Get the current URL

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
        )
)

Internal redirect

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']);

Internal redirect

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:

  • get
  • post
  • put
  • delete
  • options
  • patch
  • connect
  • trace
  • head
  • currentMethod
Example 1

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();
Example 2

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();
Example 3

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();
Example 4

You can also give a new domain name by calling the domain method.

Router :: redirect('customer.overview') -> domain('sub.example.com') -> get();
Example 5

You can chain all the methods.

Router :: redirect('customer.details') -> params([52]) -> domain('sub.example.com') -> get();

Forwarding browser

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']);
Example 1

Simulation of a Location header.

//Redrect browser to "http://example.com/customer/overview"
Router :: forward('customer.overview') -> execute();
Example 2

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(); 
Example 3

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(); 
Example 4

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".

Example 5

You can chain all the methods.

Router :: forward('customer.details') -> params([52]) -> domain('sub.example.com') -> seconds(2) -> execute();

Convert URL identifier to URL link

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.

Check if route exists

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.

Route exists
if(true === Router :: routeExists('article.view')) {
    //Route exists
}
Route exists including domain name
if(true === Router :: routeExists('article.view', 'example.com')) {
    //Route exists for givin domain
}

Check if domain exists

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
}