官术网_书友最值得收藏!

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:

Creating the structure for our project

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).

主站蜘蛛池模板: 深水埗区| 无锡市| 舒城县| 太和县| 滁州市| 耒阳市| 富宁县| 太谷县| 岱山县| 广丰县| 台湾省| 含山县| 浦城县| 南昌市| 兰考县| 延津县| 汨罗市| 扶沟县| 工布江达县| 周宁县| 泽库县| 怀仁县| 宽甸| 台中市| 正安县| 溆浦县| 莱州市| 通榆县| 沁源县| 铜川市| 婺源县| 巴彦县| 海淀区| 永登县| 江门市| 日照市| 舟山市| 板桥市| 修文县| 昌图县| 安多县|