无法为Zend Framework Module加载模块的索引操作 [英] Cannot load module's index action for Zend Framework Module

查看:99
本文介绍了无法为Zend Framework Module加载模块的索引操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为管理界面创建一个模块,但是很难使其完全响应 -到目前为止,我所做的是:我已经修改了主应用程序的引导程序加载模块目录的文件:

$modules = PROJECT_DIR . '/library/PHPLib/ZF/Modules';
define('MODULES_DIR', $modules);
$frontController->addModuleDirectory(MODULES_DIR);

该模块包含在我的外部PHP库中,该库可用于每个项目(无论是否基于ZendFramework).这是我第一次添加模块,但是以前我使用该库包含了没有问题的帮助程序类和插件.模块的结构包含在PHPLib/ZF/Modules中,如下所示:

ModAdmin/
  controller/
    IndexController.php
  model/
  view/
    includes/
    layouts/
    scripts/
      index/
        index.phtml

正如我在自己拥有的某些ZF资源中所指示的那样,IndexController.php类定义为"ModAdmin_IndexController".该脚本存在并且尚未创建布局.我不确定是否要使用一个.

因此,当我尝试在浏览器中请求ModAdmin模块的索引时,我得到404.我使用的URL是:

http://主机名/ModAdmin/

我错过了什么吗?根据诸如"ZendFramework In Action"和"ProPHP"之类的书,他们说这就是所需要的.当我读到他们说这很简单时,我几乎不能停止笑-ZendFramework中从来没有什么那么"简单-我喜欢ZF,ALOT,但它并不简单.之所以存在复杂性,是因为它是如此灵活和强大.

反正-有什么想法吗?

编辑

我想我应该补充一点,就是我早在使用Zend_Application或Zend_Tool类之前就开始使用ZF-这是在ZF明智并开始模仿RoR样式MVC之前-在任何情况下-我的项目都没有这样的原因,直到我开始研究模块之前,我什至都不知道它们的存在.话虽这么说-我没有时间安装与之相关的学习曲线,而且我对手动创建引导程序感到非常满意.

实际上-坦白地说-我创建了一个名为"padmin"的部署工具",这是一个CLI运行的php脚本,它完成了Zend_Tool的许多工作-我将其与"phing"结合使用,例如php的"ant"-无论如何-我像大多数人一样绕过它.

EDIT2

我发现该资源似乎无需使用Zend_Tool即可解决模块的添加问题,尽管遵循了它列出的所有要求,但以上述方式请求模块时,我仍然得到404代码:

http://主机名/模块名称/索引或

匹配规则指定仅在以下情况下才匹配模块 传递的控制器目录数组中存在相同名称的键 到前端控制器和调度程序.

嗯-伙计们-我就是这样做的:

    $defaultControllerDir  = PROJECT_DIR . '/application/controller';
    $modadminControllerDir = PROJECT_DIR . '/library/PHPLib/ZF/Modules/ModAdmin/controller';
$frontController->setControllerDirectory(array(
                                         'default'  => $defaultControllerDir,
                                         'modadmin' => $modadminControllerDir
                                        ));

但是-没有骰子.浏览器以404重新启动.如果前端控制器具有某种内部审核/日志记录功能,我可以打开它并查看发生了什么事情,那将是很好的选择(如果我想深入研究源代码,我想可以做到这一点)并放一些打印语句,但这太荒谬了.

这绝对让人麻木.很久以前,当我第一次学习ZF时,就发生了这种情况.据记载,当出现问题时,很难理解为什么.好吧,我再次遇到另一个问题-我遵循了所有可能的说明(我在zend_tool之前就发现了它),但没有任何效果.

我唯一要做的就是建立这样的目录结构:

modules/
  default/
    controllers/
    models/
    views/
  custom1/
    controllers/
    models/
    views/

但是这样做会完全破坏模块的目的,对吗?如果我无法将模块化模块代码保存在库中(即不在应用程序目录中,而是在外部库中),那么创建模块的意义何在!!我需要能够将我的模块代码放入我的库中:'/library/PHPLib/ZF/Modules/ModAdmin'-这是怎么回事!

编辑-响应所选答案

非常有趣-将Apache的目录设置更改为AllowOverride All之后,我得到了插件对mod-admin请求的响应,而不是404(array(3){["module"] => string(9 )"mod-admin" ["controller"] => string(5)"index" ["action"] => string(5)"index"})然后如果我禁用插件,则会出现此错误:

致命错误:未捕获的异常'Zend_Controller_Dispatcher_Exception' 带有消息指定了无效的控制器(索引)" /home/nevertap/work/nevertap/library/Zend/Controller/Dispatcher/Standard.php:248 堆栈跟踪:#0 /home/nevertap/work/nevertap/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard-> dispatch(Object(Zend_Controller_Request_Http), 对象(Zend_Controller_Response_Http))#1 /home/nevertap/work/nevertap/application/configuration/bootstrap.php(100): Zend_Controller_Front-> dispatch()#2 /home/nevertap/work/nevertap/application/configuration/bootstrap.php(110): Bootstrap-> configureFrontController()#3 /home/nevertap/www/nevertap/index.php(19):Bootstrap-> runApp()#4 {main}投入 /home/nevertap/work/nevertap/library/Zend/Controller/Dispatcher/Standard.php 在第248行

这很奇怪,因为.../Modules/mod-admin/controller/IndexController.php与.../Modules/mod-admin/view/scripts/index/index.phtml一样存在-更不用说了IndexController类适当地以"ModAdmin_IndexController"为前缀

最终编辑

好,我终于得到了它-看起来是通过加载模块目录来寻找默认的控制器目录,该目录始终被模式化为"controllers",而我的则是"controller",因此我没有使用addModuleDir加载模块,而是使用了另一个: /p>

$modadminControllerDir = PROJECT_DIR . '/library/PHPLib/ZF/Modules/mod-admin/controller';
$frontController->addControllerDirectory($modadminControllerDir, 'mod-admin');

解决方案

将模块目录重命名为"mod-admin"

mod-admin/
  controller/
    IndexController.php
  model/
  view/
   ...

您的控制器类应保留为ModAdmin_IndexController,并通过 http://hostname/mod-admin/

P.S. 并且当然仍然会这样做

$modules = PROJECT_DIR . '/library/PHPLib/ZF/Modules';
define('MODULES_DIR', $modules);
$frontController->addModuleDirectory(MODULES_DIR);

P.P.S

要查看您的请求发生了什么,您可以注册一个插件,并覆盖routeshutdown方法以var_dump这样的参数,(您的Bootstrap文件应该看起来像这样)

<?php

class TestPlugin extends Zend_Controller_Plugin_Abstract {

    public function routeShutdown(Zend_Controller_Request_Abstract $request) {
        var_dump($request->getParams());
        die();
    }

}

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { ...

    protected function _initDebugingRequest() {
       $front = Zend_Controller_Front::getInstance();
       $front->registerPlugin(new TestPlugin());
    }
...

P.P.P.S

确保已使用 sudo a2enmod rewrite 启用了重写功能,并且在/etc/apache2/sites-available/default 文件中,您也拥有类似的东西

<Directory /var/www/>
...
AllowOverride All
...
</Directory>

I am trying to create a module for an admin interface but am having trouble getting it to respond at all - What I've done so far is this: I've modified my main application's bootstrap file to load the modules directory:

$modules = PROJECT_DIR . '/library/PHPLib/ZF/Modules';
define('MODULES_DIR', $modules);
$frontController->addModuleDirectory(MODULES_DIR);

The module is contained inside my external PHP library that I re-use for every project, ZendFramework based or not. This is the first time I am adding a module, but previously I had used the library to contain helper classes and plugins with no issues. The structure of the module is contained inside the PHPLib/ZF/Modules and looks like the following:

ModAdmin/
  controller/
    IndexController.php
  model/
  view/
    includes/
    layouts/
    scripts/
      index/
        index.phtml

The IndexController.php class is defined as "ModAdmin_IndexController" as I was instructed in some of the ZF resources I own. The script is present and the layout hasnt been created yet. I am not sure if I am going to use one or not.

So, when I try to request the index of the ModAdmin module in the browser, I get 404. The URL I am using is:

http://hostname/ModAdmin/

Am I missing something? According to some books such as "ZendFramework In Action" and "ProPHP" they say that this is all that is required. When I read that they say it was this simple I almost could not stop from laughing - nothing in ZendFramework is ever ever "that" simple - I like ZF, ALOT, but its not simple. The complexity is there because it is so flexible and powerful.

Anyway - any ideas?

EDIT

I think I should add that I started using ZF long before there was a Zend_Application or Zend_Tool classes - this was before ZF got wise and started emulating RoR style MVC - and in any case - my project does not have any of this because until I started researching about modules I was not even aware of their existence. That being said - I do not have the time to mount the learning curve involved with those and I am quite content manually creating my bootstrap.

Actually - to be honest - I had created a 'deployment tool' called "padmin" which is a CLI run php script that does a lot of what Zend_Tool does - and I use it in conjunction with "phing" which is like "ant" for php - anyway - I got around it, like most people do.

EDIT2

I found this resource that appears to address the adding of modules without the use of Zend_Tool and despite following through with all of the requirements it lays out, I still get a 404 code when requesting the module in the manner described:

http://hostname/modulename/index or http://hostname/modulename/

EDIT3

Well folks - I cant express how frustrating this is. Every resource on the web is either referencing the creation of modules with Zend_Tool/Application or what I end up with only discusses modules in a very specific aspect. I need something that will help me debug why it is that the dispatch / route stage of the FrontController is not finding the module. I have set the modules directory in my bootstrap. The module has a controller and a view script. That is it - it shouldn't need anything more than that. What else does it possibly require? I've tried to add the module's controller directory individually (instead of adding it via 'addModuleDirectory') and still no dice. There is some documentation I mentioned earlier which says:

The rules for matching specify that a module will only be matched if a key of the same name exists in the controller directory array passed to the front controller and dispatcher.

Well - folks - I did just that:

    $defaultControllerDir  = PROJECT_DIR . '/application/controller';
    $modadminControllerDir = PROJECT_DIR . '/library/PHPLib/ZF/Modules/ModAdmin/controller';
$frontController->setControllerDirectory(array(
                                         'default'  => $defaultControllerDir,
                                         'modadmin' => $modadminControllerDir
                                        ));

Yet - no dice. The browser kicks back with 404. It would be nice if the front controller had some kind of internal auditing/logging capacity that I could flip on and watch exactly whats happening (and I suppose I could do that if I wanted to dig into the source and put some print statements, but that's getting ridiculous).

This is absolutely mind-numbing. This happened to me a long time ago when I was first learning ZF - and as documented as it is - when something goes wrong - its very difficult to understand why. Well I am here once again with another issue - I have followed every conceivable instruction (that I found pre-dating zend_tool) and yet nothing is working.

The only thing I havent done is to set up my directory structure like this:

modules/
  default/
    controllers/
    models/
    views/
  custom1/
    controllers/
    models/
    views/

but doing that would completely defeat the purpose of a module right? If I cannot keep my modular module code in a library (i.e. not in the application directory but in an external library) then what the hell is the point of creating the module!? I need to be able to put my module code in my library: '/library/PHPLib/ZF/Modules/ModAdmin' - what gives!

EDIT - In response to Selected Answer

So interestingly enough - after changing the Directory settings of Apache to AllowOverride All then I get your plugin's response to the request for mod-admin instead of a 404 (array(3) { ["module"]=> string(9) "mod-admin" ["controller"]=> string(5) "index" ["action"]=> string(5) "index" }) then if I disable the plugin I get this error:

Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (index)' in /home/nevertap/work/nevertap/library/Zend/Controller/Dispatcher/Standard.php:248 Stack trace: #0 /home/nevertap/work/nevertap/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 /home/nevertap/work/nevertap/application/configuration/bootstrap.php(100): Zend_Controller_Front->dispatch() #2 /home/nevertap/work/nevertap/application/configuration/bootstrap.php(110): Bootstrap->configureFrontController() #3 /home/nevertap/www/nevertap/index.php(19): Bootstrap->runApp() #4 {main} thrown in /home/nevertap/work/nevertap/library/Zend/Controller/Dispatcher/Standard.php on line 248

which is very strange because .../Modules/mod-admin/controller/IndexController.php exists as does .../Modules/mod-admin/view/scripts/index/index.phtml - not to mention the IndexController class is appropriately prefixed as "ModAdmin_IndexController"

FINAL EDIT

OK I Finally got it - looks like by loading the module directory it looked for the default controller directory which is always patterned as "controllers" and mine was "controller" so instead of loading the Module with addModuleDir i used another:

$modadminControllerDir = PROJECT_DIR . '/library/PHPLib/ZF/Modules/mod-admin/controller';
$frontController->addControllerDirectory($modadminControllerDir, 'mod-admin');

解决方案

rename your module directory to "mod-admin"

mod-admin/
  controller/
    IndexController.php
  model/
  view/
   ...

your controller class should remain ModAdmin_IndexController, and you access it by http://hostname/mod-admin/

P.S. and of course still do this

$modules = PROJECT_DIR . '/library/PHPLib/ZF/Modules';
define('MODULES_DIR', $modules);
$frontController->addModuleDirectory(MODULES_DIR);

P.P.S

to see what's going on with your request you can register a plugin and override the routeshutdown method to var_dump the params like this, (your Bootstrap file should look something like)

<?php

class TestPlugin extends Zend_Controller_Plugin_Abstract {

    public function routeShutdown(Zend_Controller_Request_Abstract $request) {
        var_dump($request->getParams());
        die();
    }

}

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { ...

    protected function _initDebugingRequest() {
       $front = Zend_Controller_Front::getInstance();
       $front->registerPlugin(new TestPlugin());
    }
...

P.P.P.S

make sure you have rewrite enabled with sudo a2enmod rewrite and also in /etc/apache2/sites-available/default file you have something like

<Directory /var/www/>
...
AllowOverride All
...
</Directory>

这篇关于无法为Zend Framework Module加载模块的索引操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆