【问题标题】:Zend framework default MVC implementationZend 框架默认 MVC 实现
【发布时间】:2010-11-08 17:05:28
【问题描述】:

我使用 Zend Framework 已经有一段时间了。自从开始阅读大量文档后,我遇到了一个问题,我想知道为什么所有示例都没有提及它。

如果我是对的,MVC 设计模式偏爱单个入口点来与应用程序交互。使用 Zend,它的 index.php 文件位于 public 文件夹中。

我尝试在 Zend 提供的默认 (v 1.09) .htaccess 中使用这种方式:

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

以及默认的 index.php 文件:

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);


$application->bootstrap()
            ->run();

但到目前为止,我一直无法使用单一入口点,我不得不复制索引文件并为其指定相关控制器的名称。这样,我就可以正常加载页面,而不是收到 500 服务器错误。

所以,我的问题是:让我的应用程序正常工作我做错了什么?这是正常的做事方式吗?

谢谢

编辑@ 13h55

我已重新配置我的服务器以通过虚拟主机而不是别名访问 zend 安装,这似乎是错误。

我的别名看起来像:

Alias /elul/ "c:\wamp\www\elul\trunk\elul\public/" 

<Directory "c:\wamp\www\elul\trunk\elul\public/">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
        Order allow,deny
    Allow from all
</Directory>

到一个看起来像这样的虚拟主机:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot "C:\wamp\www\examen_fle\trunk\examen_fle\public"
    ServerName examen.elul.local
    ErrorLog "logs/your_own-error.log"
    CustomLog "logs/your_own-access.log" common
    <directory "C:\wamp\www\examen_fle\trunk\examen_fle\public">
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
    </directory>
</VirtualHost>

由于它在本地运行,我还必须编辑 C:\WINDOWS\system32\drivers\etc 中的文件 hosts 以在文件中添加examen.elul.local

127.0.0.1       examen.elul.local

【问题讨论】:

  • 这可能很简单,因为您的 http 服务器没有解析 .htaccess 文件。 AllowOverride Apache 设置是否正确?
  • 您的mod_rewrite 设置是否正确? a2enmod rewrite 启用它 + apache 重启。
  • 到目前为止 mod_rewrite 正在工作(如果我禁用它,我会收到一个未找到的错误)以及是否有一个指向我的公用文件夹的别名。我会用一个虚拟主机试试这个,看看它是否改变了什么。

标签: model-view-controller zend-framework


【解决方案1】:

如果您更喜欢使用别名而不是虚拟主机,您可能想要使用 RewriteBase 或者您可以将基本 url 添加到前端控制器:

RewriteBase /elul/
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

或

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

应该也可以...不过我用这个:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(js|ico|gif|jpg|png|css)$ /project_name/index.php

这是一个更简单的规则,但我不确定它是否真的会影响性能或涵盖所有可能性。

【讨论】:

  • 谢谢,因为我启用了虚拟主机,所以我会坚持我的新配置,但如果我必须配置一个新项目,我一定会试试你的解决方案。
  • 我只需要在一个新服务器上设置我的网络应用程序,该服务器使用别名将 url 重定向到它们的最终位置,你的解决方案就像一个魅力(我使用了第一个)
猜你喜欢
  • 1970-01-01
  • 2012-04-23
  • 2010-11-28
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 2023-04-01
  • 2011-02-24
相关资源
最近更新 更多