DEV Community

Cover image for Part 03 - Bootstrap
Johirul Islam
Johirul Islam

Posted on

Part 03 - Bootstrap

Prepare Runnable Environment by Loading Common Dependencies

When a client’s request comes to the front controller, we want to pass the request through a middle layers instead of directly starting the process. That’s why we need the Bootstrap that will load configuration data, request information, (autoload) classes and if required it could be response for exception.

Source Code Files of this Chapter

project-root/
├── app/
│   └── Core/
│       └── Bootstrap.php                   # new file
└── public/
    └── index.php                           # modified file
Enter fullscreen mode Exit fullscreen mode

Create Bootstrap.php File

Create app/Core/Bootstrap.php file and write following code:

<?php
declare(strict_types = 1);

// load environment
$config = [
    'app_name'=> 'PHP core OOP project'
];

// route define
$route = ltrim($_SERVER['REQUEST_URI'], '/');

if ($route == "get-error") {
    // trigger a fatal error
    trigger_error("An Intentional Error Occurred.", E_USER_ERROR);
}

if ($route == 'get-exception') {
    // throw an exception
    throw new Exception("An intentional Exception Occurred.");
}

echo 'Welcome to the ' . $config['app_name'];

Enter fullscreen mode Exit fullscreen mode

Code Explain:

declare(strict_types = 1)

As we explained in Front Controller index.php file chapter, the declare(strict_types=1) cannot be set globally for the whole project It only works per file and should be placed at the top of every PHP file.

$route = ltrim($_SERVER['REQUEST_URI'], '/');

if ($route == "get-error") {
   // trigger a fatal error
   trigger_error("An Intentional Error Occurred.", E_USER_ERROR);
}

if ($route == 'get-exception') {
   // throw an exception
   throw new Exception("An intentional Exception Occurred.> ");
}

Get the route path using Global $_SERVER variable.

Based on the route triggered error and thrown exception intentionally using a hard-coded condition

$config = [
   'app_name'=> 'PHP core OOP project'
];

echo 'Welcome to the ' . $config['app_name'];

Using static configuration variable, displaying a welcome greeting message.

Inject Bootstrap into Front Controller

The front controller will only receive the request and will not contain any application logic or data. Open the public/index.php file in editor and update the code:

require __DIR__ . '/../app/Core/Bootstrap.php';
Enter fullscreen mode Exit fullscreen mode

Code Explain:

First, load and execute the bootstrap file into the front-controller.

Bootstrap injecting in the front-controller is done, now we will add the following code to the front-controller file to handle the errors and exceptions that we conditionally triggered in the bootstrap file.

// error handler
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
    echo 'Message: ' . $errno   . ' - ' . $errstr . '<br>';
    echo 'File: '    . $errfile . '<br>';
    echo 'Line: '    . $errline . '<br>';
});

// exception handler
set_exception_handler(function (Exception $e) {
    echo 'Message: ' . $e->getMessage() . '<br>';
    echo 'File: '    . $e->getFile()    . '<br>';
    echo 'Line: '    . $e->getLine()    . '<br>';
});
Enter fullscreen mode Exit fullscreen mode

Code Explain:

When an error occurs in the application, set_error_handler() will be called and it will display information about the error.

When an exception occurs in the application, set_exception_handler() will be called and it will display information about the exception.

See full code: public/index.php
<?php
declare(strict_types = 1);

// error handler
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
    echo 'Message: ' . $errno   . ' - ' . $errstr . '<br>';
    echo 'File: '    . $errfile . '<br>';
    echo 'Line: '    . $errline . '<br>';
});

// exception handler
set_exception_handler(function (Exception $e) {
    echo 'Message: ' . $e->getMessage() . '<br>';
    echo 'File: '    . $e->getFile()    . '<br>';
    echo 'Line: '    . $e->getLine()    . '<br>';
});

require __DIR__ . '/../app/Core/Bootstrap.php';

Enter fullscreen mode Exit fullscreen mode

Result

Make sure server is running or go to the project-root directory and run command php -S localhost:8000 -t public/ on the terminal.

command

  • Browse http://localhost:8000 -> Returns a welcome greeting message

output-result-1

  • Browse: http://localhost:8000/get-error -> Returns error information

output-result-2

  • Browse: http://localhost:8000/get-exception -> Returns exception information

output-result-3

Learn More

PHP's error are not exception

trigger_error():

Throwing exception:

⬇SOURCE CODE: Chapter 03

Top comments (0)