PHP code example of leadtech / boot-framework

1. Go to this page and download the library: Download leadtech/boot-framework library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

leadtech / boot-framework example snippets


// Build application
$rootDir = realpath(__DIR__ . '/..');
$app = (new \Boot\Builder($rootDir))
    ->appName('BasicApplication')
    ->optimize('tmp/cache')
    ->environment(Boot::PRODUCTION)
    ->configDir('resources/config')
    ->build()
;

$service = $app->get('my-service');
$service->doSomething();

$app = (new \Boot\Http\WebBuilder($rootDir))

    // Set application name
    ->appName('SimpleMicroService')
    
    // Set version
    ->appVersion('1.0.0')
    
    // The service ID for the HTTP service, defaults to 'http'
    ->httpServiceIdentifier('http')
    
    // Optimize performance by compiling the resolved state of the service container and routing configuration.
    // The framework will generate highly optimized classes to provide the same context. Only faster :-)
    // This optimization is ny default ignored in any other environment than production.
    ->optimize('tmp/cache')
    
    // Sets the environment (the environment 
    ->environment(Boot::DEVELOPMENT)
    
    // Add path to a config directory
    ->configDir('../../shared/config')
    
    // Add path to another config directory
    ->configDir('resources/config')

    // Add a parameter (available in the service container as %project_dir% and can be injected to other services)
    ->parameter('project_dir', $rootDir)
    
    // Sets default values for route parameters
    ->defaultRouteParams(['countryCode' => 'NL'])

    // Sets default constraints for route parameters
    ->defaultRouteRequirements(['countryCode' => 'US|EN|FR|NL'])

    // Register endpoint to get employees
    ->get('employees/{countryCode}', EmployeeService::class, 'all', new RouteOptions(
        'all-employees'
    ))

    // Register endpoint to create a new employee
    ->post('employees/{countryCode}', EmployeeService::class, 'create', new RouteOptions(
        'create-employee'
    ))

    // Register endpoint to update an employee
    ->put('employees/{countryCode}', EmployeeService::class, 'update', new RouteOptions(
        'update-employee'
    ))

    // Register endpoint to delete an employee
    ->delete('employees/{countryCode}', EmployeeService::class, 'create', new RouteOptions(
        'delete-employee'
    ))

    ->build()
;

// Handle HTTP request
$app->run();
// is equivalent to: $app->get($builder->getHttpServiceIdentifier())->handle(Request::createFromGlobals());

class EmployeeService extends AbstractService
{
    /**
     * Returns all employees
     *
     * @return array               Arrays or instances of JsonSerializable are automatically encoded as json
     */
    public function all()
    {
        // For demo purposes:
        // echo $this->getServiceContainer()->get('blaat');
        // print_r($this->getRouteMatch()->getRouteParams());
        // $request = $this->getRequest();
    
        return [
            ['id' => 1, 'firstName' => 'Jan', 'lastName' => 'Bakker', 'age' => 30],
            ['id' => 2, 'firstName' => 'Ben', 'lastName' => 'Gootmaker', 'age' => 32],
        ];
    }

    /**
     * Update an employee
     *
     * @return string              A textual response is outputted as is
     */
    public function update()
    {
        return __METHOD__;
    }

    /**
     * This method will delete an employee and send a 201 Accepted on success.
     *
     * @return Response           A regular symfony response object
     */
    public function delete()
    {
        return Response::create('ACCEPTED', 201);
    }

    /**
     * This method will add an employee and send a 201 Accepted on success.
     *
     * @return Response           A regular symfony response object
     */
    public function create()
    {
        return Response::create('ACCEPTED', 201);
    }
    
}

// Build application
$rootDir = realpath(__DIR__ . '/..');
$app = (new \Boot\Console\ConsoleBuilder($rootDir))
    ->appName('SimpleConsoleApplication')
    ->optimize('tmp/cache')
    ->environment(Boot::PRODUCTION)
    ->configDir('resources/config')
    ->configDir('src/MyPackage/resources/config')
    ->parameter('project_dir', $rootDir)
    ->parameter('some_other_variable', 123)
    ->consoleServiceIdentifier('my_custom_console_id')
    ->build()
;

// Run the application
$app->run();
console
$ php composer.phar install
console
  > php php-cs-fixer.phar fix src/Leadtech/Boot/NewAwesomeFeature/ --level=symfony