开发者

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. 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!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜