Skip to content
Dzyanis Sukhanitski edited this page Jan 12, 2023 · 1 revision

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.

View Routes

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_template is the URL template
  • $path is 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');

Assign a controller to a route

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_template is the URL template
  • $controller@action is a string with the controller and action names. The controller should be declared with namespace relatively from App\\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.

Redirect Routes

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:

  • $from is URL template that should be redirected.
  • $to is target URL.
  • $status_code is the redirection status code; by default 302.

Example:

use \Videna\Core\Route;

Route::redirect('/search-engine', 'https://google.com');

Route Parameters

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.

Regular Expression Constraints

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

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

Clone this wiki locally