Skip to content

Latest commit

 

History

History
199 lines (129 loc) · 4.71 KB

basic-routing.md

File metadata and controls

199 lines (129 loc) · 4.71 KB

🏁 Basic Routing

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 with route function to get a specific route info.

GET Method

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.

💻 Code Snippet:

Route::get('/homepage', function() {
  // ...
}, name: 'home');

POST Method

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.

💻 Code Snippet:

Route::post('/add-user', function() {
  // ...
}, name: 'add_new_user');

PUT Method

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.

💻 Code Snippet:

Route::put('/edit-user', function() {
  // ...
}, name: 'edit_user');

DELETE Method

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.

💻 Code Snippet:

Route::delete('/user/delete', function() {
  // ...
}, name: 'delete_user');

MIXED Method

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.

💻 Code Snippet:

Route::mixed(['GET', 'POST'], '/settings, function() {
  //...
}, name: 'settings');

ANY Method

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.

💻 Code Snippet:

Route::any('/homepage', function() {
  //...
}, name: 'home);

Controller Route Handler

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 from UserController class and executed/mapped on / request uri.

Static Page Rendering

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 of index.html and when using this method, fastvolt automatically fetch the first file it encounters, so if index.html comes before index.tpl, index.html will be rendered.

💻 Code Snippet:

// 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.