The Bootstrap class in the Bootstrap.php file is a core component of the framework, responsible for initializing and configuring the application. It handles the setup of essential configurations, routing, and middleware processing. Below is an overview of its key functionalities:
Namespace and Imports
?phpnamespaceSystem\Core;use System\Libraries\Logger;useException;if(!defined('ROOT_PATH')){exit('No direct access allowed.');}require_onceROOT_PATH.'/system/Helpers/Core_helper.php';load_helpers(['uri','security']);
Defines the namespace System\Core.
Imports the Logger class and the Exception class.
Checks if ROOT_PATH is defined. If not, it prevents direct access to the file.
Loads the Core_helper.php file to use the load_helpers function.
Loads the uri and security helpers.
Class Definition
Defines the Bootstrap class with protected properties $routes and $uri.
The constructor sets the timezone, configures error reporting based on the debug mode, initializes the URI, creates a Router instance, and loads the routes.
init_uri()
Initializes the URI by sanitizing it and redirecting if necessary.
Splits the URI into parts and handles language prefixes.
run()
Runs the framework.
Checks the HTTP method of the request (GET, POST, etc.).
Calls the dispatch() method to route the URI to the corresponding controller and action.
Catches and handles AppException and other errors, logs the error, and displays an error message.
loadRoutes()
Loads the routes from the Api.php and Web.php route files.
Uses the global $routes variable to store the routes.
Checks and loads the corresponding route files if they exist.
dispatch()
Routes the URI to the corresponding controller and action.
Uses Router to match the URI and HTTP method with the corresponding route.
Checks and handles middleware before calling the controller.
Checks the existence of the controller and action, throws an exception if not found.
Calls the controller and action with the corresponding parameters.
Conclusion
The methods in Bootstrap.php help initialize and route the framework, handle the URI, load the routes, and route the request to the corresponding controller and action. These methods also handle exceptions and log errors to ensure the application runs smoothly.