从具有 .htaccess 规则的文件夹中运行 Zend Framework

发布于 2024-11-01 13:27:58 字数 902 浏览 1 评论 0原文

问候。基本上,我试图从文件夹中运行 Zend Framework 应用程序(实际上是多个应用程序)——但不指定公共目录。我知道这听起来很肮脏,但我有我的理由,所以这超出了我的问题范围。

例如,这可能是我的 ZF“安装”之一:
http://www.myurl.com/project1/
(其他可能位于 /project2/ 等)

我正在使用标准 ZF 目录结构。我还熟悉在项目文件夹中放置 .htaccess 以将流量重定向到 /public/ 的各种方法,这通常用于使用 Zend Framework 的共享托管环境 - 问题是,当您的项目根目录不是基本 URL,它似乎不起作用。

具体来说,如果我将以下 .htaccess 文件放在 /project1/ 中

RewriteEngine 开启
RewriteRule ^(.*)$ public/$0 [L]

我收到一个 ZF 错误页面,指出:

指定的控制器无效(项目1)

因此似乎认为该文件夹名称是控制器。

如果我使用此处解释的更冗长的解决方案,我也会遇到同样的问题: http://www.alberton.info/zend_framework_mod_rewrite_shared_hosting.html

(显然,我将路径替换为包含“我的”路径的路径,即/project1/)

非常感谢任何帮助!

Greetings. Basically, I'm trying to run a Zend Framework application (multiple of them, actually) from within folders -- but without specifying the public directory. I know this sounds dirty but I have my reasons so it's outside the scope of my question.

For example, this may be one of my ZF "installs":
http://www.myurl.com/project1/
(Others may be at /project2/, etc.)

I'm using the standard ZF directory structure. I'm also familiar with the various methods of placing an .htaccess in the project folder to redirect traffic to /public/ that is often used for shared hosting environments with Zend Framework -- the problem is that when your project root isn't the base URL, it doesn't seem to work.

Specifically, if I place the following .htaccess file in /project1/

RewriteEngine On
RewriteRule ^(.*)$ public/$0 [L]

I get a ZF error page stating:

Invalid controller specified (project1)

So it seems to think that the folder name is a controller.

I get the same problem if I use the more lengthy solution explained here:
http://www.alberton.info/zend_framework_mod_rewrite_shared_hosting.html

(Obviously, I replace the paths with paths that contain "my" path, ie. /project1/)

Any help is much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

身边 2024-11-08 13:27:58

好吧,我之前的回答很愚蠢。尝试不同的策略。

这篇文章 的评论指出了这个问题,但是它可以通过在应用程序的前端控制器上设置 baseUrl 来处理。

我将向 application/configs/application.ini 添加一个条目(可能针对每个环境),然后在 Bootstrap 中设置它。这应该允许路由忽略 /project1/ 前缀并专注于 url 的其余部分。

更新

我会设置一个类似 application.ini 的密钥,例如:

[production]
settings.baseUrl = "/project1"

如果这因每个环境而异,那么您可以为每个环境添加一个条目:

[development:production]
settings.baseUrl = "/some/local/url/prefix/project1"

然后在application/Bootstrap.php,添加一个方法,例如:

protected function _initBaseUrl()
{
    $options = $this->getOptions();
    $baseUrl = isset($options['settings']['baseUrl']) 
        ? $options['settings']['baseUrl']
        : null;  // null tells front controller to use autodiscovery, the default
    $this->bootstrap('frontcontroller');
    $front = $this->getResource('frontcontroller');
    $front->setBaseUrl($baseUrl);
}

That oughtta do it。

OK, my previous answer was boneheaded. Trying a different tack.

A comment on this post notes this problem but that it can be handled by setting the baseUrl on the app's front controller.

I'd add an entry to application/configs/application.ini (probably per environment) and then set it in Bootstrap. That should allow the routing to ignore the /project1/ prefix and focus on the rest of the url.

Update

I would set a key like application.ini, something like:

[production]
settings.baseUrl = "/project1"

If this varies on a per-environment basis, then you could add an entry per-environment:

[development:production]
settings.baseUrl = "/some/local/url/prefix/project1"

Then in application/Bootstrap.php, add a method like:

protected function _initBaseUrl()
{
    $options = $this->getOptions();
    $baseUrl = isset($options['settings']['baseUrl']) 
        ? $options['settings']['baseUrl']
        : null;  // null tells front controller to use autodiscovery, the default
    $this->bootstrap('frontcontroller');
    $front = $this->getResource('frontcontroller');
    $front->setBaseUrl($baseUrl);
}

That oughtta do it.

一笔一画续写前缘 2024-11-08 13:27:58

根据 David 的回答,如果您不需要重写 /public 中的资源,请执行以下操作

假设您在 url domain.com/yourapplication 有一个项目,

在 /application/configs/application.ini 中创建 /.htaccess ,

RewriteEngine on

RewriteRule    ^$ public/    [L]
RewriteRule    (.*) public/$1 [L]

添加

settings.baseUrl = "/yourapplication"

到 /application /Bootstrap.php add

protected function _initBaseUrl()
    {
        $options = $this->getOptions();
        $baseUrl = isset($options['settings']['baseUrl']) 
            ? $options['settings']['baseUrl']
            : null;  // null tells front controller to use autodiscovery, the default
        $this->bootstrap('frontcontroller');
        $front = $this->getResource('frontcontroller');
        $front->setBaseUrl($baseUrl);
    }

现在,当您在浏览器中访问domain.com/yourapplication时,htaccess会将所有不是有效文件的请求重定向到domain.com/yourapplication/public,并将前端控制器设置为yourapplication。

Following on from David's answer, if you need to NOT rewrite resources in /public do the following

Say you have a project at url domain.com/yourapplication

create /.htaccess like this

RewriteEngine on

RewriteRule    ^$ public/    [L]
RewriteRule    (.*) public/$1 [L]

in /application/configs/application.ini add

settings.baseUrl = "/yourapplication"

in /application/Bootstrap.php add

protected function _initBaseUrl()
    {
        $options = $this->getOptions();
        $baseUrl = isset($options['settings']['baseUrl']) 
            ? $options['settings']['baseUrl']
            : null;  // null tells front controller to use autodiscovery, the default
        $this->bootstrap('frontcontroller');
        $front = $this->getResource('frontcontroller');
        $front->setBaseUrl($baseUrl);
    }

Now when you go to domain.com/yourapplication in a browser, htaccess will redirect all requests that aren't valid files into domain.com/yourapplication/public and set your front controller to yourapplication.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文