Skip to content

The First "Hello World"

Rougin Gutib edited this page Aug 9, 2024 · 7 revisions

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');"

Writing the Slytherin application

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

$ mkdir app
$ cd app
$ touch index.php

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

use Rougin\Slytherin\Application;

// Loads the Composer autoloader -----------
require __DIR__ . '/../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

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