In order to initialize router package, visit:
{% content-ref url="./" %} . {% endcontent-ref %}
To define a basic route, here are the HTTP verbs/methods you can use in the Router
package:
- GET
- POST
- PUT
- DELETE.
The above listed methods takes two to four arguments/parameters:
uri
: the URI to match with request uri.handler
: this parameter takes either a closure, template name or controller action that should be executed when the route is accessed.middleware
: this is the code that runs before the main application loads.name
: this is an alias name for the defined route, it it used withroute
function to get a specific route info.
This route method is used for getting http get requests, this method takes three arguments.
Route::get($uri, $handler, $middleware, $name);
-
uri
: string. -
handler
: Closure|array|string. -
middleware
: string|array|null. -
name
: string|null.
Route::get('/homepage', function() {
// ...
}, name: 'home');
This route method is used for getting form data or http post requests, this method takes three arguments:
Route::post($uri, $handler, $middleware, $name);
-
uri
: string. -
handler
: Closure|array|string. -
middleware
: string|array|null. -
name
: string|null.
Route::post('/add-user', function() {
// ...
}, name: 'add_new_user');
This route method is used for making http put requests, this method takes three arguments:
Route::put($uri, $handler, $middleware);
-
uri
: string. -
handler
: Closure|array|string. -
middleware
: string|array|null. -
name
: string|null.
Route::put('/edit-user', function() {
// ...
}, name: 'edit_user');
This route method is used for making http delete requests, this method takes three arguments:
Route::delete($uri, $handler, $middleware, $name);
-
uri
: string. -
handler
: Closure|array|string. -
middleware
: string|array|null. -
name
: string|null.
Route::delete('/user/delete', function() {
// ...
}, name: 'delete_user');
This route method is used for handling custom specified request methods, this method takes four arguments:
Route::mixed($methods, $uri, $handler, $middleware, $name);
-
methods
: array. -
uri
: string. -
handler
: Closure|array|string. -
middleware
: string|array|null. -
name
: string|null.
Route::mixed(['GET', 'POST'], '/settings, function() {
//...
}, name: 'settings');
This route method is used to handle all request methods, this method takes three arguments:
Route::any($uri, $handler, $middleware, $name);
-
uri
: string. -
handler
: Closure|array|string. -
middleware
: string|array|null. -
name
: string|null.
Route::any('/homepage', function() {
//...
}, name: 'home);
The code snippet below explains how get
route method can be used with an object as handler:
use App\Http\Controller\UserController;
Route::get('/', [UserController::class, 'index']);
Or you can shorten the process by denoting the controller as string:
Route::get('/', 'UserController@index');
In the above code snippet,
index()
method is fetched fromUserController
class and executed/mapped on/
request uri.
Get
route method can also be used to display a static page or template located at the views/
folder in the application root directory:
Note: You can use Smarty template, PHP file or HTML file as the route handler, it will be automatically fetched from views directory and rendered. Also note that you can format the handler name as
index
instead ofindex.html
and when using this method, fastvolt automatically fetch the first file it encounters, so ifindex.html
comes beforeindex.tpl
,index.html
will be rendered.
// short version
Route::get('/', 'index', name: 'home');
// full version
Route::get('/', 'index.tpl', name: 'home');
the above code snippet automatically display or map the created template:
index.tpl
on '/' request uri.