PHP without a framework: Difference between revisions

From Rixort Wiki
Jump to navigation Jump to search
No edit summary
Line 4: Line 4:


* '''Dependency injection:''' [https://php-di.org/ PHP-DI]
* '''Dependency injection:''' [https://php-di.org/ PHP-DI]
* '''PSR-15 middleware dispatcher:''' [https://github.com/relayphp/Relay.Relay Relay]
* '''PSR-15 middleware dispatcher:''' [https://route.thephpleague.com/ Route]
* '''PSR-7 implementation:''' [https://docs.laminas.dev/laminas-diactoros/ laminas-diactoros]
* '''PSR-7 implementation:''' [https://docs.laminas.dev/laminas-diactoros/ laminas-diactoros]
* '''Error handling:''' [https://github.com/filp/whoops whoops]
* '''Error handling:''' [https://github.com/filp/whoops whoops]
Line 31: Line 31:
  composer require relay/relay
  composer require relay/relay
  composer require laminas/laminas-diactoros
  composer require laminas/laminas-diactoros
composer require league/route


Create skeleton directory structure:
Create skeleton directory structure:

Revision as of 20:13, 13 March 2021

Dependencies

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

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

Add dependencies

composer require php-di/php-di
composer require filp/whoops
composer require relay/relay
composer require laminas/laminas-diactoros
composer require league/route

Create skeleton directory structure:

mkdir public src

Create Bootstrap file at src/Bootstrap.php:

<?php

declare(strict_types = 1);

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

$whoops = new Whoops\Run;
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
$whoops->register();

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';
  }
}