NSWI153: Advanced Web Applications

PHP : Monolog, Slim framework

Preliminaries

Please read this section at least one day prior to the seminar. This section outlines what you are expected to know, be able to do, or prepare in advance. Following these instructions will help you get the most out of the seminar and ensure smooth participation.

Preliminaries : Knowledge, Skills, and Competence


Before the start of the practical, you should be able to:

  • Create a PHP project using Composer.
  • Install and use a dependency with Composer.
  • Be able to use Monolog for logging.
  • Be able to create HTTP API using Slim micro framework.

Overall it is enough to go through the basic tutorial of each library.

You will work in pairs on this practical. As a result, you can split the libraries to explore with your colleague.

Preliminaries : Using your own computer


This slide applies to you only if you plan to use your own computer for the practical.

Please keep in mind, that you will work in pairs using a single computer. Thus consider bringing your own mouse and keyboard.

Before the start of the practical, make sure that:

  • You have PHP installed.
  • You can run PHP from a command line using following command:
    
              php --version
            
  • You have downloaded Composer. It is enough to download just the composer.phar file.
  • You can execute following command:
    
              php composer.phar
            

Objectives

  • Monolog
  • Slim framework
  • Pair programming
  • Book Library

Background job

Run those command before we get to exercise to download dependencies.


    php composer.phar init --no-interaction
    php composer.phar require slim/slim slim/psr7 monolog/monolog php-di/php-di
  

Demonstration: Monolog

  • Have you ever used a logging library?
  • What are the benefits of logging over just print/echo?
  • Why should you utilize logging in your project?
  • What can you expect from a logging library?
Monolog

Basic concepts

The basic utilization is explained in the using Monolog documentation.

Selected concepts you should be familiar with.

  • Log level
  • Logger
  • Handler
  • Context
  • Log format

Demonstration: Slim framework

A "better" HTTP interfaces.

Motivation

The HTTP message interfaces specify a way of interaction with HTTP. It is an alternative to using HTTP wrapper ($_GET, $_POST, etc.).

Slim is a PSR-7 compliant micro framework for PHP. To use Slim with PSR-7 we need to install PSR-7 binding.

At its core, Slim is a dispatcher that receives an HTTP request, invokes an appropriate callback routine, and returns an HTTP response. That's it.

Slim application skeleton

What are your expectations of a mature PHP web application? It is quite likely that the expectations are not uniq. You can utilize Slim framework skeleton project instead of starting from 0. Yet, this is not what we do today as the skeleton is quite complex


      php .\composer.phar create-project slim/slim-skeleton slim-framework-skeleton
    

Ask yourself: Can your package manager do this?

Slim application

As the skeleton slim application is too big, we create a new application from scratch. There is a useful Get Started guide. Here are the steps to follow:

  • Use Slim and PSR-7 binding.
    
              composer require slim/slim
              composer require slim/psr7
            
  • Put Hello world into the "./public/index.php" file.
  • You can start local server using.
    
              php -S 127.0.0.1:8090 -t .\public\
            

Demonstration: Pair programming

The driver writes the code.

The navigator reviews each line as it's written, thinks about the bigger picture, spots mistakes early, and asks questions.

Rules and expectations

  • Neither role is passive.
  • You will swap roles every 15 minutes. When it goes off, swap without negotiating!
  • No third-party (AI) assistance is allowed.
  • The driver can search, but only when navigator can not help. You both need to agree and perform the search together.

You are both owners of the code. As such you are both responsible and you will both build on it later.

Note: This is not the only way to develop code in pairs. The driver / navigator approach was modified to fit the practicals.

Exercise: Book Library

You are building a REST API for a simple Book Library. There is no real database; use a PHP arrays as your data store.

Setup

Prepare your project using following steps.


      php composer.phar init
      php composer.phar require slim/slim slim/psr7 monolog/monolog php-di/php-di
    

Download index start and save it as "./public/index.php". This is your starting file. The file also contains instrcutions.

You can start local server using.


      php -S 127.0.0.1:8090 -t .\public\
    

As PHP server reads the files with every request there is no need to stop or restart the server as you develop your application.

Continue to the next slide once you are done >>>

Debriefing

This is just the beginning.

Now you can either:

  • Improve structure of your application.
  • Continue with extended edition of this exercise.

Congratulations, if you have followed the instructions, you have just reached the end of this exercise.

Exercise: Book Library extended edition

Assignment: Book Library

GitLab: ./practical-03/


Continue with the previous code you created with your partner. The objective is to expand the functionality by extending the use of libraries. You should already be familiar with many of the concepts from the NSWI142.

Do not forget to not only deploy your application, see instruction at the end of the exercise. But also commit and push into your repository.

Continue to the next slide once you are done >>>

Monolog

Update your application so that it:

  • Use WebProcessor to add information to logs.
  • Log all messages in the default format into "./log/application.log" file.
  • Log warning and higher level messages into "./log/warning.html" file using HtmlFormatter.
  • Make sure log directory is not part of the Git repository!
  • Use dependency injection (PHP-DI) to get instance of Monolog\Logger class. Refer to best-practices for using libraries for more details.

If you struggle with PHP-DI integration, take a look at PHP definitions documentation.

Continue to the next slide once you are done >>>

Slim framework

Update your application so that it:

  • Utilize error handling with middleware.
  • Update the "/" path to use JavaScript and list all books. Make the website look reasonable, at the end this is the landing page of your application.
  • Use PHP renderer to render HTML templates. All template files should be located in "./template/" directory.
  • Create a layout for your application. The layout must include a navigation header with your custom brand name; just make something up. In addition, it must include footer with a following copyright notice.

    
              © 2025-2026 NSWI153
            
  • Use Slim bridge to integrate Slim framework with PHP-DI.

Continue to the next slide once you are done >>>

Deploy your application

Deploy your application to webik.ms.mff.cuni.cz/~{your-user-name}/nswi153/practical-03.

When running your application at webik, make sure you properly set ACL. You can do that using following command:


      setfacl -m u:www-data:rwx ./log/
    

Make sure application works as intended! You may find following resources useful.

Congratulations, if you have followed the instructions, you have just reached the end of this exercise.

Questions, ideas, or any other feedback?

Please feel free to use the anonymous feedback form.