如何在Zend Framework 2中使用自定义模块?

时间:2012-09-08 10:44:21

标签: mod-rewrite apache2 zend-framework2

我在kubuntu上使用Apache / PHP / MySQL建立了一个本地网络服务器,使用ZendSkeletonApplication开发一个Web应用程序。文档根目录设置为ZSA的公用文件夹:

<VirtualHost *>
    ServerName localhost
    ServerAdmin webmaster@localhost
    DocumentRoot "/var/www/ZendSkeletonApplication/public"
    <Directory /var/www/ZendSkeletonApplication/public>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
    </Directory>
    ErrorLog /var/log/apache2/error.log
    LogLevel warn
    CustomLog /var/log/apache2/access.log combined
    ServerSignature On
</VirtualHost>

在ZSA的公共文件夹中有一个.htaccess文件:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

请求http://localhost正确检索index.php。另外,mod_rewrite正在工作。我用

交换了最后一行
RewriteRule ^.*$ test.php [NC,L]

测试它并且它有效。

接下来,我编写了一个简单的“Hello World”模块来熟悉该体系结构。实际上,我甚至在不同的教程之后编写了两个模块。但每当我尝试请求http://localhost/helloworld时,我都会收到404错误

The requested URL /helloworld was not found on this server.

我在ZF2中仔细检查了路线。

此时我被困住了。我是一名PHP开发人员,并且对服务器管理知之甚少,因此我的设置很可能容易出错。

我想问一些我可能出错的方向。你有什么陷阱可以检查吗?我还需要提供哪些其他信息?

1 个答案:

答案 0 :(得分:0)

mod_rewrite实际上工作正常,错误出现在我读过的两个教程中。 更改了一些我无法回忆的设置后,404变为500服务器错误。只有这时我在apache错误日志中找到了一个提示。

在两个教程中,它都说在view / Modulename / Index / index.phtml创建一个模板,而它必须是view / modulename / index / index.phtml(注意小写名称)。对于所有小写路径,它现在对我有用。

错误日志中的错误消息(如果有人碰巧遇到同样的错误):

  

[星期六08月14日14:38:28] [错误] [客户端127.0.0.1] PHP致命错误:未捕获的异常'Zend \ View \ Exception \ RuntimeException',消息'Zend \ View \ Renderer \ PhpRenderer ::渲染:无法渲染模板“helloworld / index / index”;解析器无法解析为文件'/var/www/ZendSkeletonApplication/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php:451