- Phalcon 教程
- Phalcon - 主页
- Phalcon - 概述
- Phalcon - 环境设置
- Phalcon - 应用程序结构
- Phalcon - 功能
- Phalcon - 配置
- Phalcon - 控制器
- Phalcon - 模型
- Phalcon - 视图
- Phalcon - 路由
- Phalcon - 数据库连接
- Phalcon - 切换数据库
- Phalcon - 脚手架应用程序
- Phalcon - 查询语言
- Phalcon - 数据库迁移
- Phalcon - Cookie 管理
- Phalcon - 会话管理
- Phalcon - 多语言支持
- Phalcon - 资产管理
- Phalcon - 表单操作
- Phalcon - 对象文档映射程序
- Phalcon - 安全特性
- Phalcon 有用资源
- Phalcon - 快速指南
- Phalcon - 有用资源
- Phalcon - 讨论
Phalcon - 配置
Web 应用程序的 config 文件夹包括以下文件 −
- config.php
- loader.php
- services.php
config.php
按目录路径提供了数据库连接和路由的配置。
<?php /* * Modified: preppend directory path of current file, because of this file own different ENV under between Apache and command line. * NOTE: please remove this comment. */ defined('BASE_PATH') || define('BASE_PATH', getenv('BASE_PATH') ?: realpath(dirname(__FILE__) . '/../..')); defined('APP_PATH') || define('APP_PATH', BASE_PATH . '/app'); return new \Phalcon\Config([ 'database' => [ 'adapter' => 'Mysql', 'host' => 'localhost', 'username' => 'root', 'password' => '', 'dbname' => 'test', 'charset' => 'utf8', ], 'application' => [ 'appDir' => APP_PATH . '/', 'controllersDir' => APP_PATH . '/controllers/', 'modelsDir' => APP_PATH . '/models/', 'migrationsDir' => APP_PATH . '/migrations/', 'viewsDir' => APP_PATH . '/views/', 'pluginsDir' => APP_PATH . '/plugins/', 'libraryDir' => APP_PATH . '/library/', 'cacheDir' => BASE_PATH . '/cache/', 'baseUri' => '/demo1/', ] ]);
loader.php
它扩展了现有的 \Phalcon\Loader() 类。加载器类注册了 Web 应用程序所需的文件目录。
<?php $loader = new \Phalcon\Loader(); /** * We're a registering a set of directories taken from the configuration file */ $loader->registerDirs( [ $config->application->controllersDir, $config->application->modelsDir ] )->register();
services.php
这个文件关联了所有实现 Web 项目服务的功能。它实现了 Phalcon\Di 接口。它还通过加载对服务进行依赖注入。
基本上,config 文件中的 services.php 文件充当所有服务的一个容器。此接口有助于初始化所有服务,如数据库连接、设置 cookies、创建新会话或连接到 NoSQL 数据库。
<?php use Phalcon\Mvc\View; use Phalcon\Mvc\View\Engine\Php as PhpEngine; use Phalcon\Mvc\Url as UrlResolver; use Phalcon\Mvc\View\Engine\Volt as VoltEngine; use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter; use Phalcon\Session\Adapter\Files as SessionAdapter; use Phalcon\Flash\Direct as Flash; /** * Shared configuration service */ $di->setShared('config', function () { return include APP_PATH . "/config/config.php"; }); /** * The URL component is used to generate all kind of urls in the application */ $di->setShared('url', function () { $config = $this->getConfig(); $url = new UrlResolver(); $url->setBaseUri($config->application->baseUri); return $url; }); /** * Setting up the view component */ $di->setShared('view', function () { $config = $this->getConfig(); $view = new View(); $view->setDI($this); $view->setViewsDir($config->application->viewsDir); $view->registerEngines([ '.volt' => function ($view) { $config = $this->getConfig(); $volt = new VoltEngine($view, $this); $volt->setOptions([ 'compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_' ]); return $volt; }, '.phtml' => PhpEngine::class ]); return $view; }); /** * Database connection is created based in the parameters defined in the configuration file */ $di->setShared('db', function () { $config = $this->getConfig(); $class = 'Phalcon\Db\Adapter\Pdo\\' . $config->database->adapter; $connection = new $class([ 'host' => $config->database->host, 'username' => $config->database->username, 'password' => $config->database->password, 'dbname' => $config->database->dbname, 'charset' => $config->database->charset ]); return $connection; });
广告