- Learning Phalcon PHP
- Calin Rada
- 304字
- 2021-07-16 20:30:18
Creating the structure for our project
Now, we are going to create the structure for our project. In the first chapter, we created the /var/www/learning-phalcon.localhost
folder. If you have another location, go there and create the following directory structure:

Next, let's create the index.php
file that will handle our application. This file will be the default file in our web server:
<?php header('Content-Type: text/html; charset=utf-8'); mb_internal_encoding("UTF-8"); require_once __DIR__.'/../modules/Bootstrap.php'; $app = new Bootstrap('frontend'); $app->init(); ?>
In the first two lines, we set the header and internal encoding to UTF-8. This is a good practice if you are going to use special characters / diacritics. In the fourth line, we include a file named Bootstrap.php
. This file is the Bootstrap of our project, and we will create its content in a few moments. On the next lines, we create a new instance of Bootstrap with a default module (frontend
), and we initialize it.
We will need to find a way to autoload any file in our application without manually including it. We will make use of the \Phalcon\Loader
component that will register all our modules in the namespace. In the config
folder, we will create a new file called loader.php
with the following content:
<?php $loader = new \Phalcon\Loader(); $loader->registerNamespaces(array( 'App\Core' => __DIR__ . '/../modules/Core/', 'App\Frontend' => __DIR__ . '/../modules/Frontend/', 'App\Api' => __DIR__ . '/../modules/Api/', 'App\Backoffice' => __DIR__ . '/../modules/Backoffice/', )); $loader->register(); ?>
PSR
PSR is a collection of standards used in PHP development, which is supported by a group of people, the PHP Framework Interop Group. The standards include these:
- The autoloading standard
- The basic coding standard
- The coding style guide
- Logger interface
The \Phalcon\Loader
component is PSR-4 (https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md) compliant, and it helps us to load only the file that we need, when we need them. In this way, we increase the speed of our application. Meanwhile, you can find more information about this component in the official documentation (at http://docs.phalconphp.com/en/latest/reference/loader.html).