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…
Bootstrap is a fundamental concept in PHP development that sets up the environment for a web application. It loads necessary configurations, handles routing, classes, and manages exceptions.
In this chapter, we create a new Bootstrap.php file within the app/Core directory. The file begins with the declaration of strict types, ensuring that PHP treats all variables as explicit types. Next, we define an array called $config with an application name. We then extract the route from the $_SERVER superglobal variable using the REQUEST_URI key and the ltrim function.
If the route matches 'get-error', we trigger a fatal error using the trigger_error function. If the route matches 'get-exception', we throw an intentional exception using the throw new Exception function. Finally, we display a welcome message using the $config['app_name'] value.
To integrate Bootstrap into the front controller, we modify the public/index.php file. We first require the Bootstrap.php file, which sets up the environment. Next, we set up error and exception handlers using the set_error_handler and set_exception_handler functions. These handlers display appropriate error and exception information when they occur.
By following these steps, we successfully set up a basic Bootstrap system within a PHP project.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.