PHP without a framework: Difference between revisions

From Rixort Wiki
Jump to navigation Jump to search
Line 31: Line 31:
Add dependencies:
Add dependencies:


  composer require php-di/php-di
composer require php-di/php-di
composer require filp/whoops


Run composer to install dependencies and create <code>vendor</code>:
Run composer to install dependencies and create <code>vendor</code>:

Revision as of 14:15, 6 March 2021

Dependencies

The following dependencies are all actively maintained and available via Composer:

  • Dependency injection: PHP-DI
  • PSR-15 compatible dispatcher:
  • PSR-7 implementation:
  • Error handling: whoops

PHP built-in server

The PHP built-in server is sufficient for most purposes. Create a configuration file, local.ini:

error_reporting = E_ALL
display_errors = On

Start the server:

php -S localhost:8000 -t public -c local.ini

Instructions

Initialise composer project:

composer init

Create skeleton directory structure:

mkdir public src

Add dependencies:

composer require php-di/php-di
composer require filp/whoops

Run composer to install dependencies and create vendor:

composer install

Create skeleton front controller at public/index.php

<?php

declare(strict_types = 1);

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

Create HelloWorld class in src/HelloWorld.php:

<?php

declare(strict_types = 1);

namespace MyApp;

class HelloWorld
{
  public function hello() : void
  {
    echo 'Hello World';
  }
}