不使用Zend_Application的Zend Framework项目 [英] Zend Framework project without using Zend_Application
问题描述
我一直在阅读许多站点甚至此处,以提高Zend的性能框架应用程序不要在引导程序中使用Zend_Application,但是无法找到演示了此内容的站点.
I've been reading on many sites even here that in order to improve performance of Zend Framework applications is not to use the Zend_Application in bootstrap, but haven't been able to find a site that has this demonstrated.
你们知道一个描述了这种方法的地方,可能会为我提供一些代码示例吗?
Do you guys know of a place that has this method described and might provide me with some code samples?
谢谢
推荐答案
我刚刚将其放在一起:
https://gist.github.com/2822456
为完成复制以下内容.未经测试,只是一些我一般认为(!)可能有用的想法.现在,我已经逐步了解了它,我对Zend_Application,其引导程序类以及其可配置/可重用的应用程序资源有了更大的了解. ;-)
Reproduced below for completion. Not tested, just some ideas for how I think it generally (!) might work. Now that i have walked through it a bit, I have a greater appreciation for Zend_Application, its bootstrap classes, and its configurable/reusable application resources. ;-)
// Do your PHP settings like timezone, error reporting
// ..
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/_zf/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(),
)));
// Get autoloading in place
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
// Any additional configs to autoloader, like custom autoloaders
// Read config
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
// bootstrap resources manually:
// * create db adapter
// * create resource autoloaders with the mappings you need
// * etc
// Get the singleton front controller
$front = Zend_Controller_Front::getInstance();
// Set controller directory
$front->setControllerDirectory(APPLICATION_PATH . '/controllers');
// Or set module directory
$front->setModuleDirectory(APPLICATION_PATH . '/modules');
// Other front config, like throw exceptions, etc.
// ...
//
// Create a router
$router = new Zend_Controller_Router_Rewrite();
// Add routes to the router
$router->addRoute('myRoute', new Zend_Controller_Router_Route(array(
// your routing params
)));
// More routes...
// Alternatively, the routes can all be in an xml or ini file and you can add
// them all at once.
// Tell front to use our configured router
$front->setRouter($router);
// Add an plugins to your $front
$front->registerPlugin(new My_Plugin());
// other plugins...
// Dispatch the request
$front->dispatch();
也许还有一些View/ViewRenderer的工作要做.但是,正如在其他地方所指出的那样,ViewRenderer带来了不小的性能损失.如果性能是问题,那么您将要禁用ViewRenderer,并让您的动作控制器使用$this->view->render('my/view-script.phtml')
There might be some View/ViewRenderer stuff to do, as well. But as noted in other places, the ViewRenderer incurs a non-trivial performance hit. If performance is the issue, then you'll want to disable the ViewRenderer and make your action controllers call their own rendering using $this->view->render('my/view-script.phtml')
调用$front->dispatch()
时,将自动创建$request
和$response
对象.如果要在引导时执行特定于它们的操作(例如在响应的Content-Type标头中设置字符集),则可以自己创建请求/响应对象,执行所需的操作,然后将其附加到前端$front->setResponse($response);
与请求对象相同.
When you call $front->dispatch()
, the $request
and $response
objects will be created automatically. If you want to do something specific to them at bootstrap - like setting the charset in Content-Type header of the response - then you can create your request/response object yourself, do what you want to it, and then attach it to the front with $front->setResponse($response);
Same for the request object.
虽然我看到我的示例使用了Zend_Loader_Autoloader
和Zend_config_Ini
,但Padraic指出这会降低性能.下一步将通过使用数组进行配置,从框架中剥离require_once调用,注册其他自动加载器等来解决这些问题,剩下的工作留给读者...;-)
Although I see that my example uses Zend_Loader_Autoloader
and Zend_config_Ini
which Padraic notes incur performance hits. The next step would be to address those by using arrays for config, stripping require_once calls from the framework, registering a different autoloader, etc, an exercise left for the reader... ;-)
这篇关于不使用Zend_Application的Zend Framework项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!