简体   繁体   中英

ZF1 dispatch error controller from bootstrap

In my Zend Framework 1 project I am trying to add an init to Bootstrap.php that will load some configuration values and then catch an exception if the configuration value doesn't exist. After it catches an exception I want it to route to the error controller and display the error message from the exception that it caught.

Is there a way to throw an exception in Bootstrap.php in a Zend Framework 1 project and have the error handler handle it as it would if the exception was thrown from within a controller?

Update: Thanks to David Weinraub I came up with the following solution.

Bootstrap.php:

protected function _initRegistry()
{
    $options = $this->getOptions();
    $fc = Zend_Controller_Front::getInstance();
    $fc->registerPlugin(new Application_Plugin_RegistryHandler($options));
}

RegistryHandler.php:

use Application\Service\Config\Config;
use Application\Service\Config\MailConfig;

/**
 * Handles the loading of objects and values into the registry, we use a plugin
 * so exceptions can be thrown and caught with
 * Zend_Controller_Plugin_ErrorHandler.
 */
class Application_Plugin_RegistryHandler extends Zend_Controller_Plugin_Abstract
{
    /**
     * @var array
     */
    private $options;

    /**
     * @param array $options
     */
    public function __construct($options)
    {
        $this->options = $options;
    }

    /**
     * Load the config classes into registry on route startup
     * so the error controller should be loaded and ready to catch exceptions.
     * 
     * @param Zend_Controller_Request_Abstract $request
     */
    public function routeStartup(Zend_Controller_Request_Abstract $request)
    {
        $registry = Zend_Registry::getInstance();
        $mailConfig = new MailConfig($this->options);
        $config = new Config($this->options);
        $config->setMailConfig($mailConfig);
        $registry->set('config', $config);
    }
}

Any exceptions thrown from here are caught and handled with the error handler and can display a nice message like "Configuration value 'doctrine.conn.database' is missing from local.ini", I then use the registry to access these configuration values (entity manager and mail handler to be added later) from anywhere in the application.

I wish I had the authority to move this project to Zend Framework 2, much easier to work with.

Using the ErrorController to handle exceptions during bootstrap is usually problematic. After all, the ErrorController itself depends upon that bootstrapping.

If possible, can you put your config checking in a front-controller plugin running at routeStartup ? By then, the bootstrapping has already occurred and the standard ErrorHandler plugin has already been registered, so throwing an exception there should result in the ErrorController handling it.

I suppose you could try/catch the block where you check the config. Then, in the catch, you check to see if the ErrorHandler plugin has already been registered. If not, then you manually register it yourself and then re-throw the exception. Not tested, just thinking out loud.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM