* optional
public/index.php => App.php::handle => Routing::dispatch => *Middleware:before => Controller => *Middleware:after => Response
App.phpwill register all services to the DI Container, and register all the routing paths.
public/
index.php
src/
Console/
Controller/
Middleware/
ServiceProviders/
App.php
CoreServiceProvider.php
Routes.php
Tasks.php
There is 2 types of Service Provider :
CoreServiceProvider.php. This only contained the common services like :request,response,respose emitter.src/ServiceProviders/, add your new custom service here.
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 serviceHello. ThisHelloservice is a PHP class, that we will retrieve from DI container. The objective is to useHelloservice functionsayHi().
How to do it :
-
create the service :
src/Services/Hello.php -
create the service provider
src/ServiceProviders/Controller/HelloWorld.php -
update the routes file. Add new service provider to DI
container, before passed thecontainertocontrollerconstructor. See example. -
Later in
HelloWorldcontroller, retrieve serviceHellofrom container, then callsayHi()function. See example.
<?php
$route->map(
'GET',
'/hello/{name}',
[
new \PhpBootstrap\Controller\HelloWorld(
$container->addServiceProvider(new \PhpBootstrap\ServiceProviders\Controller\HelloWorld)
),
'sayHi'
]
);<?php
$hello = $this->container->get(\PhpBootstrap\Contracts\Hello::class);
$hello->sayHi();All the route list define in Routes.php.
Currently there is 2 route groups :
-
Response content-type
applicationJSONgroup. This group use middlewareapplicationJSON. -
Response content-type
text/htmlgroup. This group use middlewaretextHTML.
<?php
$route->map(
'GET',
'/',
function (\Psr\Http\Message\ServerRequestInterface $request, \PhpBootstrap\Contracts\Response $response) {
return $response->withArray(['Hello' => 'World']);
}
);<?php
$route->map(
'GET',
'/hello/{name}',
[
new \PhpBootstrap\Controller\HelloWorld($container),
'sayHi'
]
);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());php console.php list
example : run app:helloworld task.
php console.php app:helloworld
-
Create new console task example
HelloWorld.php. This class mustextends\Symfony\Component\Console\Command\Command. -
Add this new console task to the
tasks list
$application->add(new \PhpBootstrap\Console\HelloWorld());