Site icon 时鹏亮的Blog

零基础学Phalcon 21 准备初始DI接口和路由

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

本节对应书里第二章 Preparing the initial DI interface and the router
原文翻译整理如下:

在Bootstrap(引导程序)中,我们缺少两个文件:services.php和routing.php。 services.php文件将保存我们应用程序使用的全局服务的信息,而routing.php文件将保存路由信息。 现在,让我们开始在config文件夹中创建services.php文件,包含以下内容:

<?php
use PhalconLoggerAdapterFile as Logger;
$di['session'] = function () use ($config) {
$session = new PhalconSessionAdapterRedis(array(
'uniqueId' => $config->session->unique_id,
'path' => $config->session->path,
'name' => $config->session->name
));
$session->start();
return $session;
};
$di['security'] = function () {
$security = new PhalconSecurity();
$security->setWorkFactor(10);
return $security;
};
$di['redis'] = function () use ($config) {
$redis = new Redis();
$redis->connect(
$config->redis->host,
$config->redis->port
);
return $redis;
};
$di['url'] = function () use ($config, $di) {
$url = new PhalconMvcUrl();
return $url;
};
$di['voltService'] = function($view, $di) use ($config) {
$volt = new PhalconMvcViewEngineVolt($view, $di);
if (!is_dir($config->view->cache->dir)) {
mkdir($config->view->cache->dir);
}
$volt->setOptions(array(
"compiledPath" => $config->view->cache->dir,
"compiledExtension" => ".compiled",
"compileAlways" => true
));
return $volt;
};
$di['logger'] = function () {
$file = __DIR__."/../logs/".date("Y-m-d").".log";
$logger = new Logger($file, array('mode' => 'w+'));
return $logger;
};
$di['cache'] = function () use ($di, $config) {
$frontend = new PhalconCacheFrontendIgbinary(array(
'lifetime' => 3600 * 24
));
$cache = new PhalconCacheBackendRedis($frontend, array(
'redis' => $di['redis'],
'prefix' => $config->application->name.':'
));
return $cache;
};

$di变量之所以可用是因为我们在Bootstrap的_registerServices()方法中对它执行了初始化。 $di是PhalconDIFactoryDefault()的一个实例。 让我们试着理解所设置的每个组件:

  • $di['session']默认可用,不同的是由于我们想让session保存到Redis,所以我们在代码中重写了它。
  • $di['security'] 默认也是可用的,由于我们想用的安全级别比默认的更高,所以我们重写了它。我们将使用此组件来加密密码。
  • $di['redis'] 负责连接Redis服务。我们通过配置文件传递连接参数。Redis类默认可用,因为我们早在第一章就已经安装好了(php5-redis)。
  • $di['url']默认可用。之所以重写它是为了兼容老版本的Phalcon。在过去,我们无法在未定义的情况下使用它。Phalcon 1.3版本之后,该方法才和实际预期一致。
  • $di['voltService']是一个自定义的DI组件,我们将使用Volt模板引擎(稍候你将学习该模板引擎的相关知识)。
  • $di['logger']是一个自定义的DI组件,他使用PhalconLoggerAdapterFile组件。我们将使用它来记录不同的错误和警告。
  • $di['cache']也是一个自定义的DI组件,它使用Igbinary作为前端缓存,使用redis作为后端缓存。 你需要从PECL安装Igbinary,如果你没有安装,通过以下命令安装: sudo pecl install igbinary && echo 'extension=igbinary.so' | tee /etc/php5/mods-available/igbinary.ini

    请注意,你可能需要在安装Igbinary之后重新安装php5-redis。

由于我们将在Phalcon中使用一些默认情况不可用的组件,我们需要从phalcon/incubator(https://github.com/phalcon/incubator)安装它们。 Incubator是收集开发中组件的社区,这些组件不一定被包含在Phalcon之中。 我们现在需要的组件之一是PhalconCacheBackendRedis。

我们将使用Composer(https://getcomposer.org/)来管理我们的包依赖。 要安装composer,请在learning-phalcon.localhost目录下执行以下命令:

curl -s http://getcomposer.org/installer | php

上面这句不翻墙不一定能访问,自己下载文件放根目录后用php运行它:http://pan.baidu.com/s/1dFLx7ED

现在,根目录下应该有名为composer.phar的新文件。然后,通过以下指令安装phalcon/incubator:

php composer.phar require phalcon/incubator dev-master

这将安装其他依赖包,比如Swift Mailer,因此可能需要几分钟时间才能完成。如果你检查文件夹结构,将看到一个名为vendor的新目录已被创建。 这是composer的默认安装文件夹,所有软件包将存放于此。

然而,这还不够。 为了从vendor自动加载文件,我们需要修改public/index.php文件来添加composer的自动加载器。 新的index.php文件应如下所示:

<?php
header('Content-Type: text/html; charset=utf-8');
mb_internal_encoding("UTF-8");
require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__.'/../modules/Bootstrap.php';
$app = new Bootstrap('frontend');
$app->init();

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

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


与《零基础学Phalcon 21 准备初始DI接口和路由》相关的博文:

Exit mobile version