Skip to content

Latest commit

 

History

History
196 lines (143 loc) · 6.3 KB

doc.md

File metadata and controls

196 lines (143 loc) · 6.3 KB

Guide Menus

Application Flow

* optional

public/index.php => App.php::handle => Routing::dispatch => *Middleware:before => Controller => *Middleware:after => Response
  • App.php will register all services to the DI Container, and register all the routing paths.

Folder Structure

public/
  index.php
src/
  Console/
  Controller/
  Middleware/
  ServiceProviders/
  App.php
  CoreServiceProvider.php
  Routes.php
  Tasks.php

DI Container

There is 2 types of Service Provider :

  1. CoreServiceProvider.php. This only contained the common services like : request, response, respose emitter.
  2. src/ServiceProviders/, add your new custom service here.

Custom Service Provider

Question : How to add new custom service provider.

Answer : Create new php class in ServiceProviders DIR which extend \League\Container\ServiceProvider\AbstractServiceProvider

Example : We Have a controller named HelloWorld, inside this controller, we want to use service Hello. This Hello service is a PHP class, that we will retrieve from DI container. The objective is to use Hello service function sayHi().

How to do it :

  1. create the service : src/Services/Hello.php

  2. create the service provider src/ServiceProviders/Controller/HelloWorld.php

  3. update the routes file. Add new service provider to DI container, before passed the container to controller constructor. See example.

  4. Later in HelloWorld controller, retrieve service Hello from container, then call sayHi() function. See example.

example of step 3
<?php
$route->map(
'GET',
'/hello/{name}',
  [
      new \PhpBootstrap\Controller\HelloWorld(
          $container->addServiceProvider(new \PhpBootstrap\ServiceProviders\Controller\HelloWorld)
      ),
      'sayHi'
  ]
);
example of step 4
<?php
$hello = $this->container->get(\PhpBootstrap\Contracts\Hello::class);
$hello->sayHi();

Routing

All the route list define in Routes.php.

Currently there is 2 route groups :

  1. Response content-type applicationJSON group. This group use middleware applicationJSON.

  2. Response content-type text/html group. This group use middleware textHTML.

Routing Closure

<?php
$route->map(
    'GET',
    '/',
    function (\Psr\Http\Message\ServerRequestInterface $request, \PhpBootstrap\Contracts\Response $response) {
        return $response->withArray(['Hello' => 'World']);
    }
);

Routing Class Controller

<?php
$route->map(
    'GET',
    '/hello/{name}',
    [
        new \PhpBootstrap\Controller\HelloWorld($container),
        'sayHi'
    ]
);

Middleware

Middleware should be put in this src/Middleware DIR.

Middleware is used in routing dispatching process. By using middleware, we can define logic(eg: modify request, response) that needed to be executed, before or after the application process.

Example :

We have this DummyTokenChecker, which validate the request uri must contained access_token key.

URL?access_token=abcdef

If access_token is not provided in the uri, then it will return errorUnauthorized 401 response.

{
    "error":
    {
        "http_code": 401,
        "phrase": "Unauthorized"
    }
}

If valid, then the request will be continued to the HelloWorld controller.

How the Route code looks like.

<?php
$route->map(
    'GET',
    '/hello/{name}',
    [
        new \PhpBootstrap\Controller\HelloWorld(
            $container->addServiceProvider(new \PhpBootstrap\ServiceProviders\Controller\HelloWorld)
        ),
        'sayHi'
    ]
)->middleware(new \PhpBootstrap\Middleware\DummyTokenChecker());

Console

listing all console tasks

php console.php list

Run console task

example : run app:helloworld task.

php console.php app:helloworld

How to add new console task

  1. Create new console task example HelloWorld.php. This class must extends \Symfony\Component\Console\Command\Command.

  2. Add this new console task to the tasks list

 $application->add(new \PhpBootstrap\Console\HelloWorld());