Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Starting up with Silex Microframework

Starting up with Silex Microframework

1. System Steup:

Install the right php:

Install PHP7.1.

If older version of PHP is installed then upgrade it with:
Ref: https://ayesh.me/Ubuntu-PHP-7.1

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

If an error arises, then try:

sudo apt-get install software-properties-common python-software-properties

Check currently installed packages:

dpkg -l | grep php | tee packages.txt

Install PHP7.1:

sudo apt-get install php7.1 php7.1-common
sudo apt-get install php7.1-curl php7.1-xml php7.1-zip php7.1-gd php7.1-mysql php7.1-mbstring

Check if PHP has been upgraded with: php -v

Install composer:

Ref: https://getcomposer.org/download/

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

Ref: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-composer-on-ubuntu-14-04

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

If curl error (35) or (75) appears, then run:

sudo apt-get install ca-certificates

and
Ref: http://beader.me/geekbook/linux_swiss_army_knife/curl.html

sudo mkdir -p /etc/pki/tls/certs
sudo cp /etc/ssl/certs/ca-certificates.crt /etc/pki/tls/certs/ca-bundle.crt

At this point, composer should be already installed system wide.

2. Creating a Silex Project:

Create a directory for the Silex App, e.g. silex_app:
Ref: https://silex.symfony.com/download

composer require silex/silex "^2.0"

A vendor directory will be created, with all necessary resources to create a Silex App.

Then, create the file web/index.php with the following content:

<?php

// web/index.php
require_once __DIR__.'/../vendor/autoload.php';

$app = new Silex\Application();

$app->get('/hello/{name}', function ($name) use ($app) {
    return 'Hello '.$app->escape($name);
});
// ... definitions

$app->run();
$app['debug'] = true;

From Project root's directory run:

php -S localhost:8080 -t web web/index.php

Go to a browser with the url http://0.0.0.0:8080/hello/<your_name>
You should get the following HTML result in your broswer: <html><head></head><body>Hello <your_name></body></html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.