Zend Framework项目不使用Zend_Application

Rez*_*a S 4 php model-view-controller performance zend-framework

我一直在阅读上的许多网站,甚至在这里,为了提高Zend Framework的应用程序的性能是不使用Zend_Application的引导,但一直没能找到有此表现出了现场.

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

谢谢

Dav*_*aub 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();
Run Code Online (Sandbox Code Playgroud)

可能还有一些View/ViewRenderer要做的事情.但正如其他地方所指出的那样,ViewRenderer会带来非常重要的性能影响.如果性能是问题,那么您将要禁用ViewRenderer并使您的操作控制器使用调用自己的渲染$this->view->render('my/view-script.phtml')

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

虽然我看到我的例子使用Zend_Loader_AutoloaderZend_config_IniPadraic注意到性能命中.下一步是通过使用数组进行配置,从框架中剥离require_once调用,注册不同的自动加载器等来解决这些问题,为读者留下一个练习...... ;-)