Loading...
 
Slim a PHP micro framework, web application initial set up using Vagrant.

Slim Web Project - Initial Set Up

There are two aspects to a Slim web project, understanding how to use Slim Framework's objects and methods and understanding the underlying code base. This page focuses on How to Build a Web Application, Anatomy of a Micro Framework focuses on the Slim Framework source code.

References

Requirements

  • VirtualBox is a powerful x86 and AMD64/Intel64 virtualization product for enterprise as well as home use.1
  • Vagrant is a tool for building and managing virtual machine environments in a single workflow.2
  • Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.3
  • Scotch Box is a pre-configured Vagrant Box with a full array of features to get you up and running with Vagrant in no time.4
    • Apache HTTP Server Project is an effort to develop and maintain an open-source HTTP server for modern operating systems including UNIX and Windows.5

Optional

  • Scotch Box Pro Just a Dead-Simple Local LAMP/LEMP Stack for Vagrant. Making Vagrant EASY AS EATING CAKE for developers.6
    • Nginx is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server.7
  • PHPStorm is a perfect PHP IDE for working with Symfony, Drupal, WordPress, Zend Framework, Laravel, Magento, Joomla!, CakePHP, Yii, and other frameworks.8

Project Root Folder

Make a project root folder, for example mySlimApp:

$ cd ~
$ mkdir -p ~/AppDev/localdev/mySlimApp

Launch PHP Storm

Launch PHPStorm via JetBrains ToolBox - A control panel for your tools and projects.

JetBrains ToolBox

Create New Project

Use PHPStorm to create a new empty PHP project, in this example in ~/AppDev/localdev/mySlimApp

PHPStrom New Project
PHPStrom PHP Empty Project

Git

Git allows a team of people to work together, all using the same files. And it helps the team cope with the confusion that tends to happen when multiple people are editing the same files.9 Even if you are solo developer, Git is value tool, for tracking code changes.

  • From the main menu, choose VCS | Enable Version Control Integration
  • In the dialog that opens, select Git from the drop-down list and click OK.
PHPStrom Enable Version Control Integration

gitignore

There are a number of files/folders that should not be stored in the Git repo, use gitignore to intentionally untracked those files/folders.10 Open PHPStorm's embedded Terminal (Alt+F12), and download the latest JetBrains .gitignore file, using the following command:

JetBrains.gitignore
$ wget -O .gitignore https://raw.githubusercontent.com/github/gitignore/master/Global/JetBrains.gitignore
PHPStorm Terminal JetBrains.gitignore


For a Slim project, the following can be added to the existing .gitignore file.

.gitignore file
# System Files
.DS_Store
Thumbs.db

# Slim .gitignore
vendor/
.env
/logs/*
!/logs/README.md
public/tests

# Vagrant
.vagrant/

composer.phar

# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock

Create a New Directory for the Web Application Files

Create a new directory htdocs, this directory will contain all the files that will be deployed to a web server. Additional files and directories, that should not be deployed to a web server, such as the Vagrantfile, will be stored outside of the htdocs directory.

Create Additional Web Application Directories

In PHPStorm's Terminal, Alt+F12

Folder Structure
$ mkdir -p htdocs/public/{css,js,img}
$ mkdir -p htdocs/logs
$ mkdir -p htdocs/src
$ mkdir -p htdocs/tests
$ mkdir views
PHPStorm Project Directories

Scotch Box Pro - Vagrant

PhpStorm integrates with Vagrant helping you create reproducible development environments defined by VagrantFile configuration files.11

Scotch Box Pro (NGINX) is a preconfigured Vagrant Box with a full array of LEMP Stack features to get you up and running with Vagrant in no time. For this project a Linux, NGINX, MySQL, and PHP Stack will be used. See Pro Documentation - Scotch Box for additional information. Use the wget command to create the Vagrantfile from the Scotch Box Pro Git repo.: $ wget -O Vagrantfile ...

Configuring Shared Folder

Update the "mapped" folder to point to the Slim root folder.

Change Shared Folder - Vagrantfile
config.vm.synced_folder "./htdocs", "/var/www", :mount_options => ["dmode=777", "fmode=666"]

Launch Vagrant VM

To launch the VM use Vagrant command vagrant up - This command creates and configures guest machines according to your Vagrantfile.

PHPStorm Vagrant Up

Quick Vagrant / Scotch Box Pro Test

Create a simple index.html file in the mySlimApp/htdocs/public directory, for example:

index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
Hello, World
</body>
</html>


Open your favourite browser to http://192.168.33.10/ and you should see a Hello, World page.

Quick LEMP Stack Test

Create a info.php file in the mySlimApp/htdocs/public directory, with the following:

info.php
<?php

echo phpinfo();


Open your favourite browser to http://192.168.33.10/info.php and you should see:

PHP Info

Init Composer

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.12

Initialize Composer and then move composer.json to the htdocs directory.

PHPStorm Init Composer

Install Dependencies

In PHPStorm's Terminal, Alt+F12

$ vagrant ssh
$ cd /var/www

Slim Framework

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.13

$ composer require slim/slim "^3.0"

PHPUnit

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks.

$ composer require --dev phpunit/phpunit

Phan

Phan is a static analyzer for PHP that prefers to minimize false-positives.

$ composer require phan/phan

PhpFastCache

PhpFastCache A PHP library made for building reactive apps

$ composer require phpfastcache/phpfastcache

Monolog

Monolog sends your logs to files, sockets, inboxes, databases and various web services.

$ composer require monolog/monolog

Twig (Twig View)

Twig is a modern template engine for PHP

$ composer require slim/twig-view

Twig Assets Extension

Caching and compression for Twig assets (JavaScript and CSS).

$ composer require odan/twig-assets

Slim Framework CSRF protection middleware

Slim Framework CSRF protection middleware - CSRF protection applies to all unsafe HTTP requests (POST, PUT, DELETE, PATCH).

$ composer require slim/csrf

Swift Mailer

Swift Mailer integrates into any web app written in PHP, offering a flexible and elegant object-oriented approach to sending emails with a multitude of features.

$ composer require "swiftmailer/swiftmailer:^6.0"

Example composer.json

{
  "name": "interactpoint/myslimapp",
  "description": "My Slim App",
  "minimum-stability": "stable",
  "license": "APLv2",
  "authors": [
    {
      "name": "Michael K. Alber",
      "email": "malberl@example.com"
    }
  ],
  "require": {
    "slim/slim": "^3.0",
    "phpfastcache/phpfastcache": "^7.0",
    "phan/phan": "^1.1",
    "monolog/monolog": "^1.24",
    "slim/twig-view": "^2.4",
    "odan/twig-assets": "^2.0",
    "slim/csrf": "^0.8.3",
    "swiftmailer/swiftmailer": "^6.0"
  },
  "require-dev": {
    "phpunit/phpunit": "^7.5"
  }
}

Quick Slim Framework Test

Delete the existing index.html file

Delete the existing info.php file

Create a index.php file in the mySlimApp/htdocs/public directory, with the following:

index.php
<?php

require __DIR__ . './../vendor/autoload.php';

$app = new Slim\App;

$app->get('/', function ($request, $response) {
    return 'hello, world';
});

$app->run();


Open your favourite browser to http://192.168.33.10/ and you should see a hello, world page.

Commit the initial Slim installation and modules

git-add - Add file contents to the index and git-commit - Record changes to the repository

Commit the project so far, include a message, something like:

Initial Slim installation, additional PHP modules, and Vagrant configuration
PHPStorm Git Commit

Shut Down Vagrant VM

To shut down (halt) the VM use Vagrant command vagrant halt - This command shuts down the running machine Vagrant is managing.

PHPStorm Vagrant Halt

Next...

Slim Web Project - XDebug Set Up

Last edited by MichaelAlber .
Page last modified on Sunday January 20, 2019 19:35:13 UTC.