将 ZF2 站点部署到共享主机 [英] Deploy ZF2 site to shared host

查看:28
本文介绍了将 ZF2 站点部署到共享主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您将 Zend Framework 网站部署到共享主机时,通常无法将 DocumentRoot 更改为指向网站的 public/文件夹.因此,该网站的 URL 现在是 http://www.example.com/public/.这看起来不太专业,所以我想删除它.到目前为止,我一直在使用 ZF1,Rob Allen 在他的博客 http://akrabat.com/zend-framework/zend-framework-on-a-shared-host/.我试图为 ZF2 修改这个.他建议在根目录中放置一个 index.php 文件:

When you deploy a Zend Framework website to a shared host, you usually cannot change the DocumentRoot to point at the public/ folder of the website. As a result the URL to the website is now http://www.example.com/public/. This doesn't look very professional, so I'd like to remove it. Up to now I have used ZF1 and Rob Allen kindly provides a method for doing this on his blog http://akrabat.com/zend-framework/zend-framework-on-a-shared-host/ . I have tried to modify this for ZF2. He proposes placing an index.php file in the root with the line:

include 'public/index.php';

执行此操作后,http://www.example.com 打开索引页 OK 但 CSS 链接坏了.Rob 添加了一个控制器插件来将 baseUrl 重置为/public 以处理面向公众的 CSS 和图像文件等.为此,我在 ZF2 中找到了来自 Matthew Weier O' Phinney http://zend-framework-community.634137.n4.nabble.com/Setting-the-base-url-in-ZF2-MVC-td3946284.html 在这里他描述了如何设置 baseUrl.根据他的代码,我将其添加到 modules/Application/Module.php

After doing this, http://www.example.com opens the index page OK but the CSS links are broken. Rob adds a controller plugin to reset the baseUrl to /public to deal with public facing CSS and image files etc. To do this in ZF2 I found an item from Matthew Weier O' Phinney http://zend-framework-community.634137.n4.nabble.com/Setting-the-base-url-in-ZF2-MVC-td3946284.html where he describes how to set the baseUrl. Based on his code I added this to modules/Application/Module.php

class Module {
    public function onBootstrap(MvcEvent $e) {
        $config          = $e->getApplication()->getServiceManager()->get('config');
        $router          = $e->getApplication()->getServiceManager()->get('router');        
        $router->setBaseUrl($config['base_url']);
    }
}

base_url 键在 modules/Application/configs/module.config.php 中设置:

The base_url key is set in modules/Application/configs/module.config.php:

'base_url' =>'/公共'

我能够转储路由器对象并确认在此阶段正确设置了 base_url.不幸的是,现在 http://www.example.com 不再打开索引页面并给出 404 路由错误.

I was able to dump the router object and confirm that the base_url was being set correctly at this stage. Unfortunately, now http://www.example.com no longer opens the index page and gives a 404 routing error.

有没有人能告诉我我做错了什么,或者为我指明在共享托管环境中运行 ZF2 站点的正确方向?

Is anyone able to tell me what I am doing wrong or point me in the right direction for running a ZF2 site in a shared hosted environment?

推荐答案

您在使用骨架应用程序吗?

Are you using the skeleton app?

这似乎有点过头了,但肯定比这简单得多.

that seems a little over the top, surely it's lot simpler than that.

将所有内容从公共移动到根

move everything from public to the root

更改 index.php

change index.php

<?php
/**
 * This makes our life easier when dealing with paths. Everything is relative
 * to the application root now.
 */
//chdir(dirname(__DIR__));
chdir(__DIR__);

// Setup autoloading
require 'init_autoloader.php';

// Run the application!
Zend\Mvc\Application::init(require 'config/application.config.php')->run();

简单.

如果您正在运行这样的应用程序,您可能希望使用 htaccess 等阻止对某些 Zend Framework 文件夹的直接访问

If you are running an application like this you may want to block direct access to some of the Zend Framework folders using htaccess etc

这篇关于将 ZF2 站点部署到共享主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆