谢谢@David Weinraub,我使用了与您类似的插件。不过,我不得不改变一些事情,这是我的最终结果(这里的示例简化了我的一些应用程序特定的东西)
<?php
/**
* Lanch project within valid dates, otherwise show the splash page
*/
class App_Launcher extends Zend_Controller_Plugin_Abstract
{
// The splash page
private $_splashPage = array(
'module' => 'default',
'controller' => 'coming-soon',
'action' => 'index'
);
// These pages are still accessible
private $_whiteList = array(
'rules' => array(
'module' => 'default',
'controller' => 'sweepstakes',
'action' => 'rules'
)
);
/**
* Check the request and determine if we need to redirect it to the splash page
*
* @param Zend_Controller_Request_Http $request
* @return void
*/
public function preDispatch(Zend_Controller_Request_Http $request)
{
// Redirect to Splash Page if needed
if ( !$this->isSplashPage($request) && !$this->isWhiteListPage($request) && !$this->isSiteActive() ) {
// Create URL for Redirect
$urlHelper = new Zend_View_Helper_Url();
$url = $urlHelper->url( $this->_splashPage );
// Set Redirect
$front = Zend_Controller_Front::getInstance();
$response = $front->getResponse();
$response->setRedirect( $url );
}
}
/**
* Determine if this request is for the splash page
*
* @param Zend_Controller_Request_Http $request
* @return bool
*/
public function isSplashPage($request) {
if( $this->isPageMatch($request, $this->_splashPage) )
return true;
return false;
}
/**
* Check for certain pages that are OK to be shown while not
* in active mode
*
* @param Zend_Controller_Request_Http $request
* @return bool
*/
public function isWhiteListPage($request) {
foreach( $this->_whiteList as $page )
if( $this->isPageMatch($request, $page) )
return true;
return false;
}
/**
* Determine if page parameters match the request
*
* @param Zend_Controller_Request_Http $request
* @param array $page (with indexes module, controller, index)
* @return bool
*/
public function isPageMatch($request, $page) {
if( $request->getModuleName() == $page['module']
&& $request->getControllerName() == $page['controller']
&& $request->getActionName() == $page['action'] )
return true;
return false;
}
/**
* Check valid dates to determine if the site is active
*
* @return bool
*/
protected function isSiteActive() {
// We're always active outside of production
if( !App_Info::isProduction() )
return true;
// Test for your conditions here...
return false;
// ... or return true;
}
}
有一些改进的空间,但这将满足我现在的需要。附带说明一下,我不得不将函数改回 preDispatch,因为 $request 在 routeStartup 中没有可用的模块、控制器和操作名称,这是确保我们不会再次将请求重定向到启动页面所必需的(导致无限重定向循环)
(还刚刚为其他应该仍然可以访问的页面添加了一个“白名单”)