Site icon 时鹏亮的Blog

零基础学Phalcon 20 创建配置文件和引导文件

请知悉:本文最近一次更新为 7年 前,文中内容可能已经过时。

本节对应书里第二章 Creating the configuration file and the Bootstrap
原文翻译整理如下:

几乎任何应用程序都有一些将被重用的常量(数据库凭据,SMTP凭据等)。对于我们的应用程序,我们将创建一个全局配置文件。 此文件将成为PhalconConfig组件的一个实例。 切换到config目录并使用以下内容创建它:

<?php
return new PhalconConfig(array(
'application' => array(
'name' => 'Learning Phalcon'
),
'root_dir' => __DIR__.'/../',
'redis' => array(
'host' => '127.0.0.1',
'port' => 6379,
),
'session' => array(
'unique_id' => 'learning_phalcon',
'name' => 'learning_phalcon',
'path' => 'tcp://127.0.0.1:6379 weight=
),
'view' => array(
'cache' => array(
'dir' => __DIR__.'/../cache/volt/'
)
),
));

PhalconConfig组件简化了对应用程序中配置数据的访问。默认情况下,数据作为对象返回(例如,我们可以使用$config->application->name访问应用程序名称),但它也有一个魔术方法以数组形式返回数据$config->toArray()。 如果使用$config->toArray(),那么您将使用$config['application']['name']语法访问应用程序名称。 关于这个组件的另一个很酷的事实是,我们可以使用$config->merge($new_config)语法将另一个数组合并其中。

现在我们有了自动加载器和配置,让我们来设置Bootstrap(引导)文件。为此,请在modules文件夹中创建一个名为Bootstrap.php的文件,包含以下内容:

<?php
class Bootstrap extends PhalconMvcApplication
{
private $modules;
private $default_module = 'frontend';
public function __construct($default_module)
{
$this->modules = array(
'core' => array(
'className' => 'AppCoreModule',
'path' => __DIR__ . '/Core/Module.php'
),
'api' => array(
'className' => 'AppApiModule',
'path' => __DIR__ . '/Api/Module.php'
),
'frontend' => array(
'className' => 'AppFrontendModule',
'path' => __DIR__ . '/Frontend/Module.php'
),
'backoffice' => array(
'className' => 'AppBackofficeModule',
'path' => __DIR__ . '/Backoffice/Module.php'
),
);
$this->default_module = $default_module;
}
private function _registerServices()
{
$default_module = $this->default_module;
$di = new PhalconDIFactoryDefault();
$config = require __DIR__.'/../config/config.php';
$modules = $this->modules;
include_once __DIR__.'/../config/loader.php';
include_once __DIR__.'/../config/services.php';
include_once __DIR__.'/../config/routing.php';
$this->setDI($di);
}
public function init()
{
$debug = new PhalconDebug();
$debug->listen();
$this->_registerServices();
$this->registerModules($this->modules);
echo $this->handle()->getContent();
}
}

我们的Bootstrap(引导)文件扩展了我们访问PhalconMvcApplication(http://docs.phalconphp.com/en/latest/reference/applications.html)的registerModules()方法。 类构造函数注册我们的所有模块并设置默认模块。_registerServices()方法初始化DI,并引用我们的应用程序所需的文件。 最后,init()方法初始化应用程序。 在这里,我们使用PhalconDebug组件,因为我们需要能够随时调试应用程序。 该组件不应在生产环境中启用。

截至目前,我们创建了文件夹结构,配置文件,自动加载器和Bootstrap(引导程序)。 下面,我们将进一步创建服务,路由和前端模块文件。


如您从本文得到了有价值的信息或帮助,请考虑扫描文末二维码捐赠和鼓励。

尊重他人劳动成果。转载请务必附上原文链接,我将感激不尽。


与《零基础学Phalcon 20 创建配置文件和引导文件》相关的博文:

Exit mobile version