4

即使在这里,我也一直在许多站点上阅读,为了提高 Zend Framework 应用程序的性能,不要在引导程序中使用 Zend_Application,但还没有找到一个可以证明这一点的站点。

你们知道有这个方法描述的地方,并且可能会为我提供一些代码示例吗?

谢谢

4

2 回答 2

6

我只是把这个放在一起:

https://gist.github.com/2822456

转载如下以完成。未经测试,只是我认为它通常(!)可能工作的一些想法。现在我已经了解了一点,我对 Zend_Application、它的引导类以及它的可配置/可重用应用程序资源有了更大的了解。;-)

// 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')

当您调用$front->dispatch()时,将自动创建$request和对象。$response如果你想在引导时对它们做一些特定的事情——比如在响应的 Content-Type 标头中设置字符集——那么你可以自己创建你的请求/响应对象,做你想做的事情,然后将它附加到前面与$front->setResponse($response);请求对象相同。

尽管我看到我的示例使用了Zend_Loader_Autoloader哪些Zend_config_IniPadraic 笔记会导致性能下降。下一步是通过使用数组进行配置、从框架中剥离 require_once 调用、注册不同的自动加载器等来解决这些问题,这是留给读者的练习...... ;-)

于 2012-05-29T04:26:29.323 回答
2

嗨,我有点不同意在引导程序中不使用 Zend_Application,而且我还没有看到这种技术的具体示例。

就我个人而言,我认为不使用 Zend_app 引导您的应用程序没有什么好处,假设 a)您以“Zend 方式”做事并且 b)您的项目要么足够大,要么只是保证使用 Zend 框架(或任何其他框架)事情)。

虽然 Zend_App 非常适合在标准化结构中创建一致的复杂引导程序,但它不会对基线性能没有显着的性能影响。更直接的引导程序(在 Zend_App 到来之前是 ZF 的典型)要快得多,并且也可以在没有配置文件的情况下完成。

取自 Pádraic Brady 链接。

对我来说,上面没有任何意义,他基本上只是说 Zend_App 非常适合复杂的引导程序,但增加了性能损失。但这不是任何框架/框架组件的前提吗?我知道帕德莱克是一个非常聪明的人,我相信他有他的推理,但我也很想看到这个建议的例子/证据。

也许在回答您的问题时,您可以使用最新的 Zend 框架对基本应用程序进行基准测试,然后使用旧的非 Zend_App 方式从 < 1.10 开始使用 Zend 框架,但我想说虽然显然不是完美的 Zend_App 显然更快地启动大多数应用程序并运行,所以这是否值得“性能打击”取决于开发人员。

这是一个链接,它在某种程度上涉及您可能追求的内容,但参考了模块化方法(仍然很有趣,但仍然如此):

http://www.osebboy.com/blog/zend-framework-modules/

于 2012-05-29T00:36:57.177 回答