Skip to content

Defining HTTP Routes

Rougin Gutib edited this page Aug 10, 2024 · 18 revisions

← The First "Hello World" | Automagic Resolutions →

How an HTTP Route Works

Based on the previous guide, the sample code provided a definition of an HTTP route:

// app/web/index.php

// ...

// Define an HTTP route ---
$app->get('/', function ()
{
    return 'Hello world!';
});
// ------------------------

// ...

From the context in web development, an HTTP route defines which action to run from the incoming URL:

──► START
──► User requests "/" in the URL
──► Request received by Slytherin
──► Slytherin checks if the "/" request URL is a valid route
──► Returns the response "Hello world!" to the user
──► END

Note

In the application life cycle of Slytherin, the incoming request shall always return its corresponding response.

Creating an HTTP route

To create a simple HTTP route, kindly specify the URL that needs to be provided from the request first then the action that will be run if the request URL matched:

$app->{METHOD}($url, $action);

Note

The {METHOD} can be replaced with the available HTTP methods below currently being supported by Slytherin. The said HTTP methods can be found below in the next section.

// app/web/index.php

// ...

$app->get('/hello', function ()
{
    return 'Hello!';
});

// ...

From the web browser, open the link below and it should return the text Hello!:

http://localhost:8000/hello

Using route parameters

Slytherin also supports dynamic route parameters by adding a parameter with curly braces ({}) in the URL. To access the said parameter, it must also be available in the route action:

// app/web/index.php

// ...

$app->get('/hi/{name}', function ($name)
{
    return 'Hello ' . $name . '!';
});

// ...

Then open the link below from a web browser and it should return the text Hello royce!:

http://localhost:8000/hi/royce

Available HTTP Methods

In one of the provided examples above, the application defines an HTTP route of GET with an URL of "/hello". The keyword GET is one of the available HTTP methods that can be used for retrieving from and sending data to a server:

HTTP Method Intended Action or Description
GET Used for retrieving data from the server.
POST Used for sending data to the server and creates a new resource.
PUT Can be used to update an existing resource.
PATCH Same as PUT, as it also modifies an existing resource.
DELETE Used for deleting a specified resource.

To specify a different HTTP method (e.g., POST), it is simple as defining its HTTP route in the code:

// app/web/index.php

// ...

$app->delete('/users/{id}', function ($id)
{
    return 'Deletes the specified user.';
});

$app->get('/users', function ()
{
    return 'Shows list of users.';
});

$app->get('/users/{id}', function ($id)
{
    return 'Shows the specified user.';
});

$app->post('/users', function ($id)
{
    return 'Creates a new user.';
});

$app->put('/users/{id}', function ($id)
{
    return 'Updates the specified user.';
});

// ...

Note

As other HTTP methods besides GET cannot be accessed directly from a web browser, a tool such as Postman can be used in these situations.

Using Classes as HTTP routes

Previous code examples only provide route actions as anonymous functions. But Slytherin can also use simple PHP classes as its route action to improve code readability and maintainability:

// app/web/index.php

// ...

class Hallo
{
    public function greet()
    {
        return 'Hallo!';
    }
}

$app->get('/hallo', 'Hallo@greet');

// ...

Defining HTTP routes with PHP classes can also be defined as an array:

// app/web/index.php

// ...

$app->get('/hallo', [Hallo::class, 'greet']);

// ...

← The First "Hello World" | Automagic Resolutions →