この記事では、Zend Framework チュートリアルでのアプリケーションとブートストラップの使用方法を主に紹介し、サンプル、使用スキル、および関連する注意事項の形で詳細に分析しています。必要な方はこの記事を参照してください。
## この例では、Zend Framework チュートリアルでのアプリケーションとブートストラップの使用法を説明します。参考までに皆さんと共有してください。詳細は次のとおりです。 MVC アプリケーションでは、データベース リンクの初期化と確立、ビューとビュー アシスタントの構成、レイアウトの構成、関連プラグインの登録、アクション アシスタントの登録など、設定と準備作業を 1 つずつ完了する必要があります。場合によっては、いくつかの初期化操作が必要になることがありますが、場合によっては、これらの初期化が必要ない場合もあります。 Zend_Application は、これらの操作を完了するだけでなく、これらの構成および初期化タスクをより統合され、より秩序正しく、再利用性を高めることもできます。Zend_Application の使用法は 3 つのタイプに分類できます。
Zend_Application: include_paths と自動ロードを含む PHP 環境をロードし、ブート クラスをインスタンス化します。 Zend_Application_Bootstrap: ブート クラスのインターフェイスを提供します。 Zend_Application_Bootstrap_Bootstrap は、依存関係のチェックやオンデマンドでのブート リソースのロードなど、ブートが提供する必要がある最も一般的な機能を完了します。 Zend_Application_Resource は、オンデマンドのリソース読み込み機能を提供します。開発者は、必要に応じて Zend_Application_Bootstrap_Bootstrap を継承したり、Zend_Application_Bootstrap_Bootstrapper インターフェースを実装したりできます。 Zend_Application はエントリ ファイル (public/index.php など) にロードされ、ブート オプションと現在の環境構成に従ってインスタンス化されます。 ブート オプションには、指定されたブート クラス ファイルとブート クラス パスが含まれます。オプションは次のとおりです。 必須 include_paths自動ロード機能は、登録された名前空間を追加でロードします。#php.ini 初期化設定
「Bootstrap」ではない場合は、ブートストラップ クラス名を指定します
リソース プレフィックスのキーと値のペアのキーは、リソース プレフィックス名を表します
リソースのクラス名またはエイリアス
ロードされた追加の設定ファイルのパス
追加の設定オプション
オプションは配列、Zend_Config オブジェクト、または設定ファイルです。指定された場所にあります
##Bootstrapper
Zend_Application の 2 番目の機能は、アプリケーションをガイドすることです。具体的なインターフェイス API は次のとおりです。 ##interface Zend_Application_Bootstrap_Bootstrapper
{
public function __construct($application);
public function setOptions(array $options);
public function getApplication();
public function getEnvironment();
public function getClassResources();
public function getClassResourceNames();
public function bootstrap($resource = null);
public function run();
}
API は主に提供します。環境を設定し、ブート ロード用のリソースを取得し、ブート プログラムを取得した後、
Zend_Application_Bootstrap_BootstrapAbstract インターフェイスを実装するリソース メソッドは、次の規則に従う必要があります。メソッド タイプは保護されており、メソッドのプレフィックスは _init will で始まる必要があります。
リソース メソッドをロードして使用したい場合は、 bootstrap() にリソース名を追加するだけです。リソース名は _init プレフィックスを除いたリソース メソッドです。 複数のリソース メソッドをロードする場合は、配列を使用してそれらを指定できます。
例: ブートストラップ クラス:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { protected function _initFoo() { // ... } protected function _initBar() { // ... } protected function _initBaz() { // ... } }
_initFoo() を使用してのみロードします:
$bootstrap->bootstrap('foo');
_initFoo() および _initBar() を使用してロードします:
$bootstrap->bootstrap(array('foo', 'bar'));
すべてのリソース メソッドをロードして使用し、パラメーターなしで bootstrap() を使用します。
$bootstrap->bootstrap();
New first_web project
root@coder-671T-M:/mydev_src/zend_framework_learn/www#tree first_web /first_web/
§── application│ っていつ── Bootstrap.php
│ §── configs│ │ └── application.ini│ §── コントローラ 默认新建项目后,会给出如下的几个文件: 1.项目的Bootstrap first_web/ 具体代码如下: 2.配置文件 │ ├── configs 具体代码如下: 3.项目入口文件 ├── public 以上代码就是基本的使用Zend_Application方式,完成了环境变量的初始化,加载配置文件,初始化环境,加载模块,完成web应用程序的引导功能。 资源插件 资源插件只需要实现Zend_Application_Resource_Resource,或者,更简单的是,继承Zend_Application_Resource_ResourceAbstract。接口如下: 例如 加载使用资源插件 我了提供资源的重用性,可以将资源方法定义为资源插件。。 为了让bootstrap能够识别资源插件,定义资源插件时,需要实现Zend_Application_Bootstrap_ResourceBootstrapper. 接口定义了查找插件,注册插件和加载插件的api: 采用资源插件,不仅可以让资源可以重复利用,同时让bootstrap更简洁,如果要修改,新增资源也无需修改你的bootstrap。 通过实现Zend_Application_Bootstrap_BootstrapAbstract (被 Zend_Application_Bootstrap_Bootstrap 继承) ,才可以使用定义的资源插件 通过将指定的选项传递到application object and/or bootstrap,来注册使用资源插件。这些选项可能会从一个配置文件,或通过手动指定。规则是选项必须是键值对,键代表资源名称。资源名称,是资源插件类的类前缀。例如,Zend框架自带的资源类前缀“Zend_Application_Resource_”;任何以下,这都是资源的名称。例如, "FrontController"资源是个特例。他的选项比较特殊。 无论是使用自己的写的资源插件还是使用第三方的资源插件。你必须保证bootstrap能找到他们,bootstrap 内部通过 Zend_Loader_PluginLoader可以让我们只需要指定资源插件的类前缀,值为资源类的类路径便可以轻松注册资源插件。 例如,如果编写的资源插件存放在APPLICATION_PATH/resources/ 下,类前缀为My_Resource. 可以使用如下方法注册加载: 在应用程序中比可以使用指定目录下的资源。 和资源方法类似,通过使用 the bootstrap() 方法可以加载资源插件。加载单一,多个,全部的资源插件的配置方式也类似. 例如: 资源注册表 为了避免资源的重复注册,导致不必要的浪费Zend_Application_Bootstrap_BootstrapAbstract 提供了一个本地注册表对象存储这些资源对象.当你想要存放一个资源的时候,只需要在方法中返回这个资源即可。 为了灵活性,注册表是作为一个内部“容器”存在的。只要是对象都可以存入到容器中。资源名称对应为容器的属性。默认情况下,可以通过Zend_Registry获取实例使用,也可以自定义其他对象。 setContainer() and getContainer() 方法可用于操纵容器本身。getResource($resource) 可用于获取一个指定资源。hasResource($resource) 可以检查资源是否已经注册存在 例如,注册一个view资源: 资源的相关操作: 请注意:注册表容器是不是全局的。这意味着你需要通过访问的bootstrap来获取资源。 Zend_Application_Bootstrap_Bootstrap提供了 run() , 执行了 run() 之后,它会注册为前端控制器参数的“bootstrap”,通过他可以获取路由器,分发器,插件和动作控制器。 具体使用方法: 为了防止重复注册加载资源方法和插件或一些资源可能依赖于其他资源。为了解决这两个问题,Zend_Application_Bootstrap_BootstrapAbstract提供了一个简单的依赖性跟踪机制。 如前所述,所有的资源 - 无论是方法或插件 - 是通过 bootstrap($resource)加载运行的,其中 $resource是资源的名称,或者资源名称数组,或者为空,为空表示加载运行所有资源。 以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网! 相关推荐: Zend
Framework中Zend_View组件的用法解析 ゼンド
Framework バリデータ Zend_Validate の使用状況分析
│ │ §── ErrorController.php
│ │ └── IndexController.php
│ §── モデル
│ └── ビュー
│ §── ヘルパー
│ and-#
│docs ###││··ックス
# ├#├###テスト
§── アプリケーション
│ └── コントローラー
│ └── IndexControllerTest.php
§── bootstrap.php
§── ライブラリ
└── phpunit。 xml
16 ディレクトリ、11 ファイル
zend フレームワークの新しいバージョンでは、Zend_Application と Bootstrap が導入されています。 Zend_Application は、再利用可能なリソースのブートストラップ、汎用およびモジュール式のブートストラップ クラス、および依存関係チェックを提供します。また、デフォルトで PHP 環境変数と自動ロード機能を設定する役割もあります。
├── application
│ ├── Bootstrap.php<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
}
│ │ └── application.ini[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
│ └── index.php<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
interface Zend_Application_Resource_Resource
{
public function __construct($options = null);
public function setBootstrap(
Zend_Application_Bootstrap_Bootstrapper $bootstrap
);
public function getBootstrap();
public function setOptions(array $options);
public function getOptions();
public function init();
}
class My_Resource_View extends Zend_Application_Resource_ResourceAbstract
{
protected $_view;
public function init()
{
// Return view so bootstrap will store it in the registry
return $this->getView();
}
public function getView()
{
if (null === $this->_view) {
$options = $this->getOptions();
$title = '';
if (array_key_exists('title', $options)) {
$title = $options['title'];
unset($options['title']);
}
$view = new Zend_View($options);
$view->doctype('XHTML1_STRICT');
$view->headTitle($title);
$view->headLink()->appendStylesheet('/css/site.css');
$view->headScript()->appendfile('/js/analytics.js');
$viewRenderer =
Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer'
);
$viewRenderer->setView($view);
$this->_view = $view;
}
return $this->_view;
}
}
interface Zend_Application_Bootstrap_ResourceBootstrapper
{
public function registerPluginResource($resource, $options = null);
public function unregisterPluginResource($resource);
public function hasPluginResource($resource);
public function getPluginResource($resource);
public function getPluginResources();
public function getPluginResourceNames();
public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader);
public function getPluginLoader();
}
$application = new Zend_Application(APPLICATION_ENV, array(
'resources' => array(
'FrontController' => array(
'controllerDirectory' => APPLICATION_PATH . '/controllers',
),
),
));
$application = new Zend_Application(APPLICATION_ENV, array(
'pluginPaths' => array(
'My_Resource' => APPLICATION_PATH . '/resources/',
),
'resources' => array(
'FrontController' => array(
'controllerDirectory' => APPLICATION_PATH . '/controllers',
),
),
));
// Execute one:
$bootstrap->bootstrap('FrontController');
// Execute several:
$bootstrap->bootstrap(array('FrontController', 'Foo'));
// Execute all resource methods and plugins:
$bootstrap->bootstrap();
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initView()
{
$view = new Zend_View();
// more initialization...
return $view;
}
}
// Using the has/getResource() pair:
if ($bootstrap->hasResource('view')) {
$view = $bootstrap->getResource('view');
}
// Via the container:
$container = $bootstrap->getContainer();
if (isset($container->view)) {
$view = $container->view;
}
class FooController extends Zend_Controller_Action
{
public function init()
{
$bootstrap = $this->getInvokeArg('bootstrap');
$view = $bootstrap->getResource('view');
// ...
}
}
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initRequest()
{
// Ensure the front controller is initialized
$this->bootstrap('FrontController');
// Retrieve the front controller from the bootstrap registry
$front = $this->getResource('FrontController');
$request = new Zend_Controller_Request_Http();
$request->setBaseUrl('/foo');
$front->setRequest($request);
// Ensure the request is stored in the bootstrap registry
return $request;
}
}
以上がZend Framework でのアプリケーションとブートストラップの使用法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

BestappRoachforseminginphpisusingthephpmailerlibrarydueToitsReliability、featurrichness、andeaseofuse.phpmailerSupportssmtpは、detairederorhandlingを提供します

依存関係注射(DI)を使用する理由は、コードのゆるい結合、テスト可能性、および保守性を促進するためです。 1)コンストラクターを使用して依存関係を注入します。2)サービスロケーターの使用を避け、3)依存関係噴射コンテナを使用して依存関係を管理する、4)依存関係を注入することでテスト可能性を向上させる、5)注入依存性を回避、6)パフォーマンスに対するDIの影響を考慮します。

phpperformancetuningisucial cuseenhancess andandandadsand。

bestpracticesforsendingemails securlyinphpinclude:1)sutureconsmttarttlsencryptionとの使用の使用、2)検証およびサンシジン化のinputStopReventinjectuctacks、3)adinitivedinitivedInemailsopenslsl、4)adlinglinglingemailoaに

tooptimizephpapplicationsforporformance、usecaching、databaseoptimization、opcodecaching、andserverconfiguration.1)cachingwithedatedatedatafethtimes.2)最適化バイズビーインデキシング、readedandandandwriteoperations.3)

依存関係の依存性、テスト可能性、および維持可能性の依存性の依存性の依存性、および維持可能性は、エクステルンド依存性を維持する可能性があります

PHPパフォーマンスの最適化は、次の手順を通じて実現できます。1)スクリプトの上部にrequire_onceまたはinclude_onceを使用して、ファイルの負荷数を減らすことができます。 2)プリプロセシングステートメントとバッチ処理を使用して、データベースクエリの数を減らします。 3)OpCodeキャッシュのOpCacheを構成します。 4)PHP-FPM最適化プロセス管理を有効にして構成します。 5)CDNを使用して静的リソースを配布します。 6)コードパフォーマンス分析には、XdebugまたはBlackfireを使用します。 7)配列などの効率的なデータ構造を選択します。 8)最適化実行のためのモジュラーコードを記述します。

opcodeCachingsificlyprovesppherformanceBycachingCompiledCode、reducingServerloadandResponsetimes.1)itStoresPhpCodeInMemory、バイパス補助補強団体


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

mPDF
mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

メモ帳++7.3.1
使いやすく無料のコードエディター

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境
