Routing on the web determines what is displayed on the web and decides actions based on routes.
Routes can be registered in the web.php
file, which is already created. Each route includes the request URL, request method, and the action to take if the route matches the specified URL and method.
A view route is used to render a view file directly on the web when a specific route URL is visited. To create a view route, edit the web.php
file. If it is not included in the render.php
file, include it using include_once "web.php";
or write the route directly in render.php
.
Example:
web.php
<?php
use PhpSlides\Router\Route;
use PhpSlides\Router\view;
Route::view("/index", "::Index");
The Route::view
function takes two parameters:
- The request URL to use (e.g.,
/index
). - The view file to render (e.g.,
::Index
).
The view file follows the ::
pattern to include files in the views directory. For example, ::Index
represents the Index.psl
file in the views directory.
Navigate to http://localhost:2200/index
in the browser to see the output of the Index.psl
file.
A GET route can be registered to handle GET request methods. Example:
web.php
Route::get("/index", function () {
return "Index Page";
});
Here:
- The first parameter is the request URI (
/index
). - The second parameter is a closure function that handles the request.
When a user navigates to /index
, the string "Index Page" is returned.
A POST route can be registered to handle POST request methods, usually for form submissions.
Example:
Route::post("/submit", function () {
return "Form Submitted";
});
A PUT route is usually used to update information in a database. Example:
Route::put("/update", function () {
return "Update Successful";
});
A PATCH route is used for partial updates. Example:
Route::patch("/partial-update", function () {
return "Partial Update Successful";
});
A DELETE route is used to remove information from a database or destroy sessions. Example:
Route::delete("/delete-user", function () {
return "User Deleted";
});