-
Notifications
You must be signed in to change notification settings - Fork 0
04. Routing
We made routing as simple as possible, with the shortest way of routes declaration.
All what you need is to register all routes at the file /App/configs/routes.php.
If your route only needs to return a view (for example, a static page), you may use the Route::view method:
Route::view(string $url_template, string $path): object
Here:
-
$url_templateis the URL template -
$pathis the path to the View, relatively from application/App/Views/folder.
Example. To show view /App/Views/index.php:
use \Videna\Core\Route;
Route::view('/home', 'index.php');
If some logic is required before showing the view, you need to assign your custom controller and action for the route. For that, you need to use the method depending on the request type:
// The HTTP GET method is used to read (or retrieve) a representation of a resource:
Route::get(string $url_template, string $controller@action): object
// The POST method is most often utilized to create new resources:
Route::post(string $url_template, string $controller@action): object
// The PATCH method applies partial modifications to a resource:
Route::patch(string $url_template, string $controller@action): object
// DELETE is used to delete a resource identified by filters or ID:
Route::delete(string $url_template, string $controller@action): object
// The PUT method replaces all current representations of the target resource with the request payload:
Route::put(string $url_template, string $controller@action): object
Here:
-
$url_templateis the URL template -
$controller@actionis a string with the controller and action names. The controller should be declared with namespace relatively fromApp\\Controllers\\.
Example. To run controller Dashboard from the folder /App/Controllers/Admin/Dashboard.php and default action:
use \Videna\Core\Route;
Route::get('/dashboard', 'Admin\\Dashboard@Index');
Note: Do not forget to declare the required View in the controller action.
If you are defining a route that redirects to another URI, you may use the Route::redirect method:
Route::redirect(string $from, string $to, ?int $status_code): void
Here:
-
$fromis URL template that should be redirected. -
$tois target URL. -
$status_codeis the redirection status code; by default 302.
Example:
use \Videna\Core\Route;
Route::redirect('/search-engine', 'https://google.com');
Sometimes you will need to capture segments of the URI within your route.
For example, you may need to show a article with the specific ID from the URL. You may do so by defining route parameters:
use \Videna\Core\Route;
Route::add('/blog/{article_id}', 'Blog@ShowArticle');
Now, you can get access to parameter article_id in your controller using router's method get:
use \Videna\Core\Router;
$article_id = Router::get('article_id');
You may define as many route parameters as required by your route.
You may constrain the format of your route parameters using the where method on a route instance. The where method accepts the name of the parameter and a regular expression (in array) defining how the parameter should be constrained.
Examples:
use \Videna\Core\Route;
Route::add('/admin/get-user/{name}', 'Admin\\Dashboard@GetUser')->where(['name' => '[A-Za-z]+']);
Route::add('/admin/get-user/{id}','Admin\\Dashboard@GetUser')->where(['id' => '[0-9]+']);
Route::add('/admin/get-user/{id}/{name}', 'Admin\\Dashboard@GetUser')->where(['id' => '[0-9]+', 'name' => '[a-z]+']);
Named routes let define the current route name wherever in controller. You may specify a name for a route by chaining the name method onto the route definition.
Examples:
use \Videna\Core\Route;
Route::view('/', 'index')->name('home');
Route::view('/{lang}/', 'index')->where(['lang' => 'ru|en'])->name('home');
Route::add('/dashboard', 'Admin\\Dashboard@Index')->name('dashboard');
Route::add('/{lang}/dashboard', 'Admin\\Dashboard@Index')->where(['lang' => 'ru|en'])->name('dashboard');
Now, you can get access to route name:
- in any controller using static property
$name:
use \Videna\Core\Route;
$currentRouteName = Route::$name;
- in any view, using property
name:
<?= $route->name ?>