![](/img/trans.png)
[英]how to check if file exists using zend validator - zend framework 2.0
[英]Zend Framework check if a controller exists or not
我正在寻找标题中提到的问题的解决方案。
我有ROUTER,有一个订单,我检查控制器是否存在,并且找不到解决方案。
我做了这样的事情
class Plugin_Router extends Zend_Controller_Plugin_Abstract {
public function routeStartup (Zend_Controller_Request_Abstract $request)
{
$dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
if (!$dispatcher->isDispatchable($request)) {
// Controller exists
// Exit ();
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('/:catid', new Zend_Controller_Router_Route('/:catid', array(
'module' => 'default' ,
'controller' => 'profile' ,
'action' => '' // Check your action and controller
)));
}
}
}
并且它不起作用还有另一种解决方案?
尚不清楚此插件应该做什么。 您已将逻辑放入routeStartup()
,该逻辑发生在路由发生之前 。 因为尚未确定要分配的模块/控制器/动作,所以isDispatchable()
总是会在此处返回false。
我会说将逻辑移到routeShutdown()
( 在路由之后运行),但是您的代码的目的似乎是在路由失败时添加新路由,这将无法实现任何目的。 您可能需要重新考虑您的方法。
您可以在分发之前更改请求对象中的参数,这可能是您想要执行的操作。
我找到了一半答案:
public function routeStartup(Zend_Controller_Request_Abstract $request)
{
$url = explode("/", substr($_SERVER['REQUEST_URI'], 1));
$path = APPLICATION_PATH . '/controllers/' . ucfirst(strtolower($url[0])) .
'Controller' . '.php';
if (!file_exists($path)) {
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute(':catid', new Zend_Controller_Router_Route(':catid', array(
'module' => 'default',
'controller' => 'profile',
'action' => 'index' // Check your action and controller
)));
}
}
}
如果有人在ZEND上有有效的代码,则想听听....
经过一些测试后,我可以向您提出这个建议(这受Dispatcher and Front Controller的启发):
public function routeStartup(Zend_Controller_Request_Abstract $request){
{
//For feeding modules, controllers actions and parameters of $request
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->setParams(Zend_Controller_Front::getInstance()->getParams());
$router->route($request);
// get Module and Controller names
$dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
$controller = $request->getControllerName();
$default = $dispatcher->formatControllerName($controller);
$module = $request->getModuleName();
$controllerDirs = $dispatcher->getControllerDirectory();
$found = false;
$fileSpec = '';
if ($dispatcher->isValidModule($module)) {
if (class_exists($default, false)) {
$found = true;
} else {
$moduleDir = $controllerDirs[$module];
$fileSpec = $moduleDir . DIRECTORY_SEPARATOR . $dispatcher->classToFilename($default);
if (Zend_Loader::isReadable($fileSpec)) {
$found = true;
}
}
}
if ($found) {
// The controller exists
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.