Skip to content

Latest commit

 

History

History
69 lines (58 loc) · 3.27 KB

File metadata and controls

69 lines (58 loc) · 3.27 KB

Initial Setup

The first step would be for us to create a bootstrap file. This file is where we will initialize our application,
and add the service providers containing the services we want the application to load into the container. In this example, we will use /bootstrap/app.php. After the entrypoint file has been created, we will want to create a new $app instance so that we may build and configure the application.

<?php

// import the library:
// let's first bring in the FrameworkFactory application manager
 use FrameworkFactory\Application;

// create the app instance: 
// the basePath argument is required, and should point to the
// main directory of your project
$app = Application::build(basePath: __DIR__ . '/../'); 

// configure the application: 
// continue reading to see how to manage app configuration
// ...

// adding providers: 
// continue reading to see how to manage service providers
// ...

// fire the app up: 
// this final step will get everything bootstrapped and finalize
// the initialization process
$app->fire();

Once the bootstrap file has been created, the application is almost ready to go. Next we will have to create and add service providers to the application in order to register them within the container.

Service Providers

Once we have created our Service Classes and Service Providers, we can add them to our application. This can be done by calling the withProviders() method on the $app instance. The withProviders method accepts an array of service providers that we want to load into our application.

<?php
use App\Providers\MessageServiceProvider;

$app->withProviders([
    MessageServiceProvider::class
]);

Auto-discovery

Service Providers can be auto-discovered and do not need be added to the withProviders() method, and the withProviders() method can be omitted. In order to achieve this, all auto-discoverable providers need to live within the App\Providers namespace. Any auto-discoverable providers must either end with ServiceProvider, or Provider to be properly discovered - EG: LoggerServiceProvider or LoggerProvider. Any classes that do not contain either suffix will be ignored by the application bootstrap process and will not be added to the providers list.

The main application namespace and its corresponding directory can be customized upon the creation of the $app instance. It is important to note that the appDirectory parameter is relative to the assigned baseDir path.

<?php
 use FrameworkFactory\Application;

$app = Application::build(basePath: __DIR__ . '/../', appNamespace: 'MyApp', appDirectory: 'my-app');

Now any classes within the MyApp\Providers directory that end with either Provider or ServiceProvider will be automatically added and their services will be loaded into the container - EG: MyApp\Providers\LoggerServiceProvider or MyApp\Providers\LoggerProvider.