简体   繁体   English

使用.htaccess规则从文件夹中运行Zend Framework

[英]Running Zend Framework from within a folder with .htaccess rules

Greetings. 问候。 Basically, I'm trying to run a Zend Framework application (multiple of them, actually) from within folders -- but without specifying the public directory. 基本上,我试图从文件夹中运行Zend Framework应用程序(实际上是其中的多个应用程序),但是没有指定公共目录。 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": 例如,这可能是我的ZF“安装”之一:
http://www.myurl.com/project1/ http://www.myurl.com/project1/
(Others may be at /project2/, etc.) (其他可能位于/ project2 /等)

I'm using the standard ZF directory structure. 我正在使用标准的ZF目录结构。 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. 我还熟悉在项目文件夹中放置.htaccess来将流量重定向到/ public /的各种方法,该方法通常用于Zend Framework的共享托管环境-问题是,当您的项目根目录不是基本网址,它似乎不起作用。

Specifically, if I place the following .htaccess file in /project1/ 具体来说,如果我将以下.htaccess文件放在/ project1 /

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

I get a ZF error page stating: 我得到一个ZF错误页面,说明:

Invalid controller specified (project1) 指定了无效的控制器(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 如果使用此处说明的更长的解决方案,则会遇到相同的问题: http : //www.alberton.info/zend_framework_mod_rewrite_shared_hosting.html

(Obviously, I replace the paths with paths that contain "my" path, ie. /project1/) (显然,我将路径替换为包含“我的”路径的路径,即/ project1 /)

Any help is much appreciated! 任何帮助深表感谢!

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. 关于此帖子的评论指出了此问题,但可以通过在应用程序的前端控制器上设置baseUrl来解决。

I'd add an entry to application/configs/application.ini (probably per environment) and then set it in Bootstrap . 我将添加一个条目到application/configs/application.ini (可能是每个环境),然后在Bootstrap进行设置。 That should allow the routing to ignore the /project1/ prefix and focus on the rest of the url. 这应该允许路由忽略/project1/前缀,并专注于其余url。

Update 更新资料

I would set a key like application.ini , something like: 我将设置像application.ini这样的键,类似:

[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: 然后在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. 那应该做。

Following on from David's answer, if you need to NOT rewrite resources in /public do the following 根据David的回答,如果您不需要在/ public中重写资源,请执行以下操作

Say you have a project at url domain.com/yourapplication 假设您在url domain.com/yourapplication中有一个项目

create /.htaccess like this 像这样创建/.htaccess

RewriteEngine on

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

in /application/configs/application.ini add 在/application/configs/application.ini中添加

settings.baseUrl = "/yourapplication"

in /application/Bootstrap.php add 在/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);
    }

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. 现在,当您在浏览器中转到domain.com/yourapplication时,htaccess会将所有不是有效文件的请求重定向到domain.com/yourapplication/public,并将您的前端控制器设置为您的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM