Advanced
Web Applications


PHP, Composer, Slim Framework

Objectives

  • Package Manager
  • Using libraries
  • Logging
  • PHP PSR-7
  • Slim Framework

Running PHP

webik.ms.mff.cuni.cz

Available at https://webik.ms.mff.cuni.cz/~{username}

ssh -p 42222 {username}@webik.ms.mff.cuni.cz
      

localhost

There is PHP installed on lab computers.

php -S 127.0.0.1:8070
      

Package Manager / Package Manager

Composer is A Dependency Manager for PHP.

  • How to install?
  • Is it working?
    composer -V
    Is this always enough?
  • How to initialize a project?
    composer init
  • What is composer.json file?
  • How to use psr-4 autoloading?
  • How to install/update dependencies from composer.json file?
    composer update

Assignment: Autoloading

./practical-01/autoloading

Create application entry point index.php, no need for .htaccess for now. Create a mock of customer services. The service should provide a method to get all customers.

Use composer's autoloading to load the service from index.php, call the method and return the data.

Logging

  • What is logging?
  • What logging frameworks do you know?
  • What is this?
    Monolog image

Assignment: Logging with Monolog

./practical-01/monolog

Add Monolog to your project.

Use Monolog 3.5 (PHP > 8.1). Log all info and above logs to file ./log/info.log. Mind file system permissions on webik (ACL). Use following command to grant write permission to PHP.


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

Add WebProcessor.

Add another handler to file ./logs/info.html. Use HtmlFormatter. Open the file using web-browser of choice.

Bonus: Store logs in CouchDB.

A "better" HTTP interfaces

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.


composer require slim/slim:"4.*"
composer require slim/psr7
  

Slim application

./practical-01/slim-framework

Create a simple web application using Slim framework with two pages.

The home page ("/") contains short information about your application and a link to a form page ("/form"). In addition if a URL query argument "name" is set, it prints the value of the name somewhere at the page.

At the form page user can fill in an HTML form to create a new product. The form contains: product name (string), product code (string), and product price (number). User can submit the form using POST method to the same URL (action=""). Form data must be send in body of the request! As a response user gets back JSON with the content of the form. You do not need to address issue with page reloading.

Put HTML content into extra files (templates). Use PHP renderer to render the templates.

Add error handling using middleware.

Resources

Resources bellow may help you with the assignment from this lecture. They are not a replacement of the personal attendance for the seminar.

Questions, ideas, or any other feedback?

Please feel free to use the anonymous feedback form.