Skip to content

The First "Hello World"

Rougin Gutib edited this page Sep 4, 2024 · 7 revisions

← What is Slytherin? | Defining HTTP Routes →

Installation

To implement a simple Hello World application with Slytherin, kindly create a new directory first for the tutorial project (e.g., Hogwarts):

$ mkdir Hogwarts

Then open the newly created directory (e.g., Hogwarts) and install the Slytherin package using Composer:

$ cd Hogwarts
$ composer require rougin/slytherin

Note

This step requires Composer to be installed on the local machine. Kindly see the instructions in its Download page for its installation:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Getting the "Hello world!"

After installing the Slytherin package in the current directory, create a directory named app/web then a PHP file named index.php:

$ mkdir app
$ cd app
$ mkdir web
$ cd web
$ touch index.php

From the index.php file, copy the following code below for showing the Hello world! text in the web browser:

// app/web/index.php

use Rougin\Slytherin\Application;

// Load the Composer autoloader ----
$root = dirname(dirname(__DIR__));

require "$root/vendor/autoload.php";
// ---------------------------------

// Initialize the Slytherin application ---
$app = new Application;
// ----------------------------------------

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

// Run the Slytherin application ---
echo $app->run();
// ---------------------------------

Running the web server

Once the code is pasted in the index.php file, execute the code below to the Terminal to start the PHP built-in web server:

$ php -S localhost:8000 -t app/web

Lastly, open a new tab in the current web browser then access the link below. This should show the text Hello world! from the web browser:

http://localhost:8000

← What is Slytherin? | Defining HTTP Routes →