Routing
Build routes
Basic
In config/routes.php
, you will see a variable $scope
is used to define routes. A scope can have a number of child scopes, there is a single root scope, just like a
tree.
You can use the provided API to set up the routes with different HTTP methods.
Rise\Router\Scope::options($path, $handler, [$name])
Rise\Router\Scope::get($path, $handler, [$name])
Rise\Router\Scope::head($path, $handler, [$name])
Rise\Router\Scope::post($path, $handler, [$name])
Rise\Router\Scope::put($path, $handler, [$name])
Rise\Router\Scope::delete($path, $handler, [$name])
Rise\Router\Scope::trace($path, $handler, [$name])
Rise\Router\Scope::connect($path, $handler, [$name])
They all have the same parameter list.
$path
: A string to match with the request path. It can contain variables enclosed in braces, e.g.: 'products/{id}'
.
$handler
: A string consists of two parts separated by a dot. The first part is the class name and the second part is the method name. e.g.: 'App\Handlers\Blog.show'
$name
: This is optional. Route name can be used to generate URL.
Some examples:
$scope->get('/', 'App\Handlers\Home.index', 'root');
$scope->get('/blogs', 'App\Handlers\Blog.list');
$scope->get('/blogs/{id}', 'App\Handlers\Blog.show');
$scope->post('/blogs', 'App\Handlers\Blog.create');
$scope->put('/blogs/{id}', 'App\Handlers\Blog.update');
$scope->delete('/blogs/{id}', 'App\Handlers\Blog.delete');
Child Scope
Rise\Router\Scope::createScope(Closure $function)
can be used to create a child scope. This is useful for grouping some similar routes. Examples of usage are shown in the next section.
Prefix
Rise\Router\Scope::prefix(string $prefix)
can set the common prefix of the path in a scope. e.g.:
$scope->createScope(function ($scope) {
$scope->prefix('/blogs');
$scope->get('', 'App\Handlers\Blog.list');
$scope->get('/{id}', 'App\Handlers\Blog.show');
$scope->post('', 'App\Handlers\Blog.create');
$scope->put('/{id}', 'App\Handlers\Blog.update');
$scope->delete('/{id}', 'App\Handlers\Blog.delete');
});
It also supports route parameters, so you can do something like this:
$scope->createScope(function ($scope) {
$scope->prefix('/blogs');
$scope->get('', 'App\Handlers\Blog.list');
$scope->post('', 'App\Handlers\Blog.create');
$scope->createScope(function ($scope) {
$scope->prefix('/{id}');
$scope->get('', 'App\Handlers\Blog.show');
$scope->put('', 'App\Handlers\Blog.update');
$scope->delete('', 'App\Handlers\Blog.delete');
});
});
Namespace
Rise\Router\Scope::namespace(string $namespace)
can set the common namespace in a scope. e.g.:
$scope->namespace('App\Handlers');
$scope->get('/', 'Home.index');
$scope->get('/blogs', 'Blog.list');
Middlewares
Rise\Router\Scope::use(array $middlewares)
is used to set middlewares in a scope. e.g.:
$scope->use(['Rise\Middlewares\Session.run']);
$scope->get('/', 'App\Handlers\Home.index');
$scope->get('/blogs', 'App\Handlers\Blog.list');
Generate Routes
Rise\Router\UrlGenerator::generate(string $name, [array $params])
will return the URL of a named route. e.g.:
In config/routes.php
:
$scope->get('/', 'App\Handlers\Home.index', 'root');
$scope->get('/products/{id}', 'App\Handlers\Product.show', 'product.show');
In src/App/Handlers/Home.php
:
namespace App\Handlers;
use Rise\Response;
use Rise\Router\UrlGenerator;
class Home {
public function index(Response $response, UrlGenerator $urlGenerator) {
$response->redirect($urlGenerator->generate('product.show', ['id' => 1])); // Redirect to http://localhost:3000/products/1
}
}