Architecture Concepts
Things You Should Know
- Entry point for all HTTP requests and CLI scripts is public/index.php.
- Mandatory configuration for all HTTP requests and CLI scripts is located in public/index.php.
- You can see recommended project structure here, but it can be different depending on configuration.
- Framework throws exceptions for everything. When PHP functions emit errors, we catch it and send it to log.
- You are not forced to use our stuff, so you can write your own caching, log, mail, session, response or routing handlers. Or you can develop your own and combine with our stuff.
- Use modules to logically separate parts of your system. Otherwise you'll end up in classes with thousands of lines of code.
- Validate all input parameters on all requests.
- Avoid writing raw database queries for CRUD operations.
- Do not repeat yourself.
Without philosophy and not too much text, it works like this:
How Things Work
HTTP requests
After you pointed your web server's directory root to public
folder:
- Web server runs
public/index.php
- this is entry point for every request because you should have pretty URLs enabled. - First thing that
index.php
does, it includes thevendor/autoload.php
which is generated by Composer. It is possible to use framework without Composer, but it's not recommended because you won't have a system compatible with the PHP ecosystem, so we won't document this option. index.php
callsKoldy\Application::useConfig()
method where you should pass all configuration about your project, such as site domain, where is your application folder, custom configs directory, timezone, route class, some security options, log config and etc. Check mandatory configuration docs to see all possibilities.- After that,
index.php
simply callsKoldy\Application::run()
and now framework takes over using the configuration you provided in step before.
After this step, flow might vary depending on the config you provided, but let's use an example of some standard configuration which might look like this:
'site_url' => 'http://example.com',
'assets' => [
'static' => '/static'
],
'paths' => [
'application' => null,
'storage' => null,
'view' => null,
'modules' => null,
'scripts' => null,
'configs' => "{$projectRoot}/configs/"
],
'routing_class' => '\Koldy\Route\DefaultRoute',
'routing_options' => [
'always_restful' => false
]
You can read about configuration here.
DefaultRoute
is searching for the controller and method inside the controller depending on the request URI and the method type.- Framework will return some response when it gets return value from the called action in the controller. If there's
no method or controller, framework will throw
\Koldy\Response\Exception\NotFoundException
which should be handled by yourExceptionHandler
or it'll use its own default handler.
Everything else depends on what is happening in the action in the controller. Framework won't load any additional file that is not needed. For example, if you're not using database in the controller's method, then framework won't load any configuration for database nor any other class needed for the database connections.
PHP Scripts / CLI
When script is called, framework will still execute public/index.php
so your script will use the same configuration
as your HTTP requests. Framework will then look for the script file depending on its name and it'll execute it.
There is nothing in the system that should use echo
, print
or similar functions and you should never use those
functions as well! Use Log instead. When running the script, you can see the output from Log
usage if
Koldy\Log\Adapter\Out
is set and enabled in public/index.php.