Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework project without using 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?

Thanks

like image 868
Reza S Avatar asked Feb 21 '23 08:02

Reza S


2 Answers

I just threw this together:

https://gist.github.com/2822456

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();

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

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.

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... ;-)

like image 138
David Weinraub Avatar answered Feb 23 '23 00:02

David Weinraub


Hi I disagree somewhat with the not using Zend_Application in bootstrap and no I have yet to see a concrete example of this technique.

Personally I don't see the benefit in not using Zend_app for bootstrapping your application, assuming a) Your doing things 'the Zend way' and b) your project is either big enough or just simply warrants using the Zend framework (or any for that matter).

While Zend_App is great for creating consistent complex bootstraps within a standardised structure, it doesn’t come without a significant performance hit to baseline performance. A more direct bootstrap (typical of ZF until Zend_App arrived) is far faster and can also be done without configuration files.

Taken from Pádraic Brady link.

Well to me the above does not make sense, he basically just said Zend_App is great for complex bootstraps, but adds a performance hit. But isn't that the premise for ANY framework / framework component? I know Padraic is a very clever guy and I'm sure he has his reasoning, but I too would love to see examples / evidence of this suggestion.

Perhaps in answer to your question, you could benchmark a basic app using the latest Zend framework and then use Zend framework from < 1.10 using the old non Zend_App way, but I would say although clearly not perfect Zend_App is clearly faster to get most app's up and running, so whether this is worth 'the performance hit' is I guess up to the developer(s).

Here is a link which somewhat goes into what you may be after but makes reference to a modular approach (still interesting, none the less):

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

like image 28
Steve H Avatar answered Feb 22 '23 22:02

Steve H