Zend_Application - 第一部分 - 开始Zend Framework之旅

本文介绍使用ZendFramework进行开发时的配置方法,包括配置的位置、内容及个性化设置方式,并举例说明数据库配置与目录结构调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Zend Framework进行开发有两种模式。一种将其简单的作为一个个相互独立的组件进行利用(基于其低耦合度),别一种则将其视为框架,进行系统开发。

如果选择第二种,则最好使用Zend_Application简化并开始我们的程序。Zend_Application的官方解释是“提供了一个可重用资源的引导,通用和模块化的引导类和依赖检查。 同时默认负责设置 PHP 环境变量和自动加载功能。”。

从官方的解释可以看出,Zend_Application的主要作用是配置应用程序,当最终调用Zend_Application::run()(内部调用Zend_Controller_Front::dispatch())后,Zend_Application好像突然消失了,一切都回归到你自己写程序时在调用Zend_Controller_Front::dispatch()后要做的事。

既然Zend_Application的主要作用是配置,我们可能提出以下几个问题:

1.我们可以在哪几个地方进行的配置?

2.我们可以设置哪些内容?

3.如何进行具体的设置,以符合我们个性化的需要?

下面我一一解答这几个问题,最后说一说Zend_Application的调用堆栈。

1.我们可以在哪几个地方进行的配置?

我们大概可以从三个地方进行配置index.php,Bootstrap.php,application.ini。

2.我们可以设置哪些内容?

a.index.php中可以设置的内容:

$application = new Zend_Application( APPLICATION_ENV,//设置php环境 APPLICATION_PATH . '/configs/application.ini'//配置文件位置);

b.Bootstrap.php中,程序会自动调用类似_initYourMethod的方法,用以定制或初始化你的程序。

c.application.ini中的配置相对复杂,大概有四类配置参数:

第一类:位于library/Zend/Application.php:

比如在application.ini中输入:phpSettings.display_errors = 0,则在setOptions中会进入

if (!empty($options['phpsettings'])) {
$this->setPhpSettings($options['phpsettings']);
}并进行相应处理。

public function setOptions(array $options) { if (!empty($options['config'])) { if (is_array($options['config'])) { $_options = array(); foreach ($options['config'] as $tmp) { $_options = $this->mergeOptions($_options, $this->_loadConfig($tmp)); } $options = $this->mergeOptions($_options, $options); } else { $options = $this->mergeOptions($this->_loadConfig($options['config']), $options); } } $this->_options = $options; $options = array_change_key_case($options, CASE_LOWER); $this->_optionKeys = array_keys($options); if (!empty($options['phpsettings'])) { $this->setPhpSettings($options['phpsettings']); } if (!empty($options['includepaths'])) { $this->setIncludePaths($options['includepaths']); } if (!empty($options['autoloadernamespaces'])) { $this->setAutoloaderNamespaces($options['autoloadernamespaces']); } if (!empty($options['autoloaderzfpath'])) { $autoloader = $this->getAutoloader(); if (method_exists($autoloader, 'setZfPath')) { $zfPath = $options['autoloaderzfpath']; $zfVersion = !empty($options['autoloaderzfversion']) ? $options['autoloaderzfversion'] : 'latest'; $autoloader->setZfPath($zfPath, $zfVersion); } } if (!empty($options['bootstrap'])) { $bootstrap = $options['bootstrap']; if (is_string($bootstrap)) { $this->setBootstrap($bootstrap); } elseif (is_array($bootstrap)) { if (empty($bootstrap['path'])) { throw new Zend_Application_Exception('No bootstrap path provided'); } $path = $bootstrap['path']; $class = null; if (!empty($bootstrap['class'])) { $class = $bootstrap['class']; } $this->setBootstrap($path, $class); } else { throw new Zend_Application_Exception('Invalid bootstrap information provided'); } } return $this; }

第二类:用于配置resource组件的参数(如Zend_Application_Resource_Frontcontroller,所有可用的resource可以在library/Zend/Application/Resource目录下找到。)如在application.ini中输入resources.FrontController.moduledirectory = APPLICATION_PATH "/modules",则会在Zend_Application_Resource_Frontcontroller内部调用Zend_Controller_Front::addModuleDirectory。Zend_Application_Resource_Frontcontroller的另外可选参数参见以下代码。其它resource组件的配置类推。

/** * Initialize Front Controller * * @return Zend_Controller_Front */ public function init() { $front = $this->getFrontController(); foreach ($this->getOptions() as $key => $value) { switch (strtolower($key)) { case 'controllerdirectory': if (is_string($value)) { $front->setControllerDirectory($value); } elseif (is_array($value)) { foreach ($value as $module => $directory) { $front->addControllerDirectory($directory, $module); } } break; case 'modulecontrollerdirectoryname': $front->setModuleControllerDirectoryName($value); break; case 'moduledirectory': $front->addModuleDirectory($value); break; case 'defaultcontrollername': $front->setDefaultControllerName($value); break; case 'defaultaction': $front->setDefaultAction($value); break; case 'defaultmodule': $front->setDefaultModule($value); break; case 'baseurl': if (!empty($value)) { $front->setBaseUrl($value); } break; case 'params': $front->setParams($value); break; case 'plugins': foreach ((array) $value as $pluginClass) { $stackIndex = null; if(is_array($pluginClass)) { $pluginClass = array_change_key_case($pluginClass, CASE_LOWER); if(isset($pluginClass['class'])) { if(isset($pluginClass['stackindex'])) { $stackIndex = $pluginClass['stackindex']; } $pluginClass = $pluginClass['class']; } } $plugin = new $pluginClass(); $front->registerPlugin($plugin, $stackIndex); } break; case 'returnresponse': $front->returnResponse((bool) $value); break; case 'throwexceptions': $front->throwExceptions((bool) $value); break; case 'actionhelperpaths': if (is_array($value)) { foreach ($value as $helperPrefix => $helperPath) { Zend_Controller_Action_HelperBroker::addPath($helperPath, $helperPrefix); } } break; default: $front->setParam($key, $value); break; } } if (null !== ($bootstrap = $this->getBootstrap())) { $this->getBootstrap()->frontController = $front; } return $front; }

第三类:通过resource组件间接配置相应组件。如用Zend_Application_Resource_View配置Zend_View。原因是在Zend_Application_Resource_View中实例化Zend_View时,Zend_Application_Resource_View会将从application.ini中传来的有关视图的参数传递给Zend_View的构造函数,从而间接的配置了Zend_View。如resources.view.scriptPath = APPLICATION_PATH "/views/bbs/scripts"最终调用的是Zend_View的addScriptPath。

第四类:所有在Zend_Application_Bootstrap_Bootstrap,Zend_Application_Bootstrap_BootstrapAbstract,Zend_Application_Bootstrap_Bootstrapper,Zend_Application_Bootstrap_ResourceBootstrapper中以set开头的方法。如要设置appnamespace则可在application.ini中输入:“appnamespace = "Application"”,依据是28-32行。

/** * Set class state * * @param array $options * @return Zend_Application_Bootstrap_BootstrapAbstract */ public function setOptions(array $options) { $this->_options = $this->mergeOptions($this->_options, $options); $options = array_change_key_case($options, CASE_LOWER); $this->_optionKeys = array_merge($this->_optionKeys, array_keys($options)); $methods = get_class_methods($this); foreach ($methods as $key => $method) { $methods[$key] = strtolower($method); } if (array_key_exists('pluginpaths', $options)) { $pluginLoader = $this->getPluginLoader(); foreach ($options['pluginpaths'] as $prefix => $path) { $pluginLoader->addPrefixPath($prefix, $path); } unset($options['pluginpaths']); } foreach ($options as $key => $value) { $method = 'set' . strtolower($key); if (in_array($method, $methods)) { $this->$method($value); } elseif ('resources' == $key) { foreach ($value as $resource => $resourceOptions) { $this->registerPluginResource($resource, $resourceOptions); } } } return $this; }

3.如何进行具体的设置,以符合我们个性化的需要?

a.index.php:在文件开头加入“define('APPLICATION_ENV', 'development');”以进入调试模式;

b.以下给出application.ini配置的几个例子,主要是教你怎么在application.ini写配置(因为我不知道你的具体要求):

例一:如何配置mysql数据库?

答:在application.ini中加入如下内容:

resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = 123456
resources.db.params.dbname = dbname

例2:如何建立如下目录结构?

/application

/modules

/bbs

/blog

/controllers

/IndexController.php

/ErrorController.php

/models

/DbTable

/Article.php

/templates

/bbs

/blog

/filters

/helpers

/scripts

/index

/index.phtml

/library

/Zend

/public

/index.php

答:在application.ini中加入如下内容(这里只说明如何设置modules目录和templates目录):

a.设置modules目录

resources.FrontController.moduledirectory = APPLICATION_PATH "/modules"(内部调用Zend_Controller_Front::addModuleDirectory)

resources.FrontController.defaultModule = "blog"

b.设置templates目录

resources.view.basePath = APPLICATION_PATH "/views"
resources.view.scriptPath = APPLICATION_PATH "/views/bbs/scripts"(内部调用Zend_View::addScriptPath)
resources.view.scriptPath = APPLICATION_PATH "/views/blog/scripts"

关于resource组件的配置需要专门一篇文章详细说明:Zend_Application -第二部分 - resource组件的使用与配置

另外,想知道Zend_Application的运行流程请进入:Zend_Application -第三部分 - Zend_Application的调用堆栈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值