Skip to content
StefansArya edited this page Apr 21, 2019 · 6 revisions

You may need to import this feature on top of your code.

use Scarlets\Route;
use Scarlets\Route\Serve;

All router should be placed on /routes/ folder
The router priority is:

  1. status.php (HTTP status)
  2. api.php (API Router)
  3. web.php (Web Router)

Force HTTPS

Redirect user to https protocol

Route::forceSecure();

HTTP Status Route

This feature will help you to define returned data when any HTTP status was happened.

Route::status($code, $callable);

# Example
Route::status(404, function(){
    Serve::raw("404 Not Found");
});

Route::status(503, function(){
    # return true; // This will allow user to bypass maintenance message
    Serve::raw("System Maintenance");
});

Route by Request Method

When you're using the router, you can define your match pattern.
The match are started with the index parameter
And followed by:

  • : (Regex Pattern)
  • ? (Optional Pattern)

So it would be like:

  • /user/{0}/home
  • /quiz/{0?} <- optional pattern
  • /name/{0:[A-Za-z]+} <- regex pattern

The zero 0 means it should be passed to the first parameter of callable function..

Or you could also match all path like below

Route::get('/user/{*}', function($all){
    /* ... */
});

Routing Request

The available route in this library is

  • get
  • post
  • delete
  • put
  • options
  • match (custom method)
  • any (This route match any method)
Route::get($url, $callable, $middleware = false);

# Example
# The parameter index for example below is `0` and the regex `[A-Za-z]+`
Route::get('/text/{0:[A-Za-z]+}', function($text = ['world']){
    // Serve::raw("Hello, $text[0]");
    return "Hello, $text[0]";
});

You could also routing with your own HTTP method.

Route::method($method, $url, $callable, $middleware = false);

Namespace Router group

When you have deep namespace for your project
you can use this feature to simplify the usage.
Having nested function with other routing feature is also allowed.

Route::namespaces('App\Http\Controllers', function(){
    // Inside this scope the namespace is "App\Http\Controllers"

    // Call 'route' function on "User\Home" Class
    Route::get('/user/{0}', 'User\Home::route');
});

URL Prefix group

Define URL prefix for every router inside the function scope.

Route::prefix('admin', function(){
    Route::get('users', function(){
        // Matches The "/admin/users" URL
        Serve::raw("Hi admin!");

        // Or route to User List
        Route::route('list.users');
    });
});

Domain Router

Route::domain('{0}.framework.test', function($domain){
    Route::get('/home/{0}', function($query){
        // Will be available on "*.framework.test" domain
    });
});

Middleware Router group

This will be useful if you have a block of route.
Maybe like routes for logged in user or limited page request.

Route::middleware('limit:2,60', function(){
    Route::get('limit', function(){
        Serve::raw("Limited request");
    });
});

You could also set the middleware from request method's router

Route::get('/limited/page', function(){
    Serve::raw("Limited request");
}, 'limit:2,60');

Registering middleware for router

It's better if you define the Middleware in /app/Middleware.php. Instead of manually adding like below. The code below is just an example.

Route\Middleware::$register['limit'] = function($request = 2, $seconds = 30){
    $total = Cache::get('request.limit', 0);

    if($total < $request){
        // Set expiration when it's the first request only ($total == 0)
        $expire = $total === 0 ? $seconds : 0;

        // Put the request count on cache
        Cache::set('request.limit', $total + 1, $expire);

        // Continue request
        return false;
    }

    // Block request
    else{
        Serve::status(404);
        return true;
    }
};