【问题标题】:Zend Framework on shared hostingZend Framework 共享主机
【发布时间】:2010-11-10 01:48:18
【问题描述】:

我是 Zend 框架的新手。我想知道如何在共享主机上实现 zend 框架。由于zend框架文件夹结构 所有视图文件都放在“public”文件夹中。

假设

"/" 是我的主根文件夹,public 就像 “/公共”

这样 url 就变成了“http://site/public/. .. .bla bla...”

这对吗?

或者还有其他方法吗?

我没有创建虚拟主机的任何权限。

那该怎么办?

我希望你能理解我的问题。如果没有,请问我。

谢谢!

【问题讨论】:

    标签: zend-framework shared-hosting directory-structure


    【解决方案1】:

    设置虚拟主机通常在 httpd.conf 或 extra/httpd-vhosts.conf 中完成。如果您使用的是 httpd-vhosts.conf,请确保该文件包含在您的主 httpd.conf 文件中。一些 Linux 发行版(例如:Ubuntu)打包 Apache,以便配置文件存储在 /etc/apache2 中,并在 /etc/apache2/sites-enabled 文件夹中为每个虚拟主机创建一个文件。在这种情况下,您可以将下面的虚拟主机块放入文件 /etc/apache2/sites-enabled/zf2-tutorial。

    确保 NameVirtualHost 已定义并设置为“*:80”或类似名称,然后按照以下方式定义虚拟主机:

    Setting up the virtual host is usually done within httpd.conf or extra/httpd-vhosts.conf. If you are using httpd-vhosts.conf, ensure that this file is included by your main httpd.conf file. Some Linux distributions (ex: Ubuntu) package Apache so that configuration files are stored in /etc/apache2 and create one file per virtual host inside folder /etc/apache2/sites-enabled. In this case, you would place the virtual host block below into the file /etc/apache2/sites-enabled/zf2-tutorial.
    
    Ensure that NameVirtualHost is defined and set to “*:80” or similar, and then define a virtual host along these lines:
    
     <VirtualHost *:80>
         ServerName zf2-tutorial.localhost
         DocumentRoot /path/to/zf2-tutorial/public
         SetEnv APPLICATION_ENV "development"
         <Directory /path/to/zf2-tutorial/public>
             DirectoryIndex index.php
             AllowOverride All
             Order allow,deny
             Allow from all
         </Directory>
     </VirtualHost>
    
    Make sure that you update your /etc/hosts or c:\windows\system32\drivers\etc\hosts file so that zf2-tutorial.localhost is mapped to 127.0.0.1.

    确保更新您的 /etc/hosts 或 c:\windows\system32\drivers\etc\hosts 文件,以便将 zf2-tutorial.localhost 映射到 127.0.0.1。

    【讨论】:

    • 在共享主机中不要编辑和添加虚拟主机。
    【解决方案2】:

    我最近遇到了这个问题,通过这个帖子我能够找到解决方案。我正在使用zf2。所以我所要做的就是将与 zend 相关的文件放在一个我们可以命名为 src 的目录中。该目录将驻留在公共目录或根目录中(例如/www

    1. 在“src”目录中创建一个 .htaccess 文件并添加 Deny from All 指令。

    2. 在索引文件中,通过执行以下操作将工作目录更改为 src 文件夹:

      define('SRC_ROOT', 'src');
      chdir(SRC_ROOT);
      

    ...你很高兴!

    【讨论】:

      【解决方案3】:

      还有Rob Allen wrote a good solution

      我正在使用 Zend Framework 2,它对我有用。 虽然我还没有完成上述链接中的“参考面向公众的文件”部分。

      我刚刚将 layout.phtml 中的代码更改为:

      $basePath = $this->basePath();
      

      到:

      $basePath = $this->basePath();
      if (defined('RUNNING_FROM_ROOT')) {
          $basePath .= 'public/';
      }
      

      【讨论】:

      • Хорошее решение。 А вы не знаете как можно установить этот basePath так, чтобы на всё приложение он был одинаковый? При таком решении придется в каждом виде, где есть вызов $this-&gt;basePath(), прописывать этот код。 А хотелось бы в одном месте где-то задать его и везде бы он был один и тот же。
      • 这将需要编辑所有 phtml 文件 :( 任何更短的方法可以更快地解决问题?
      【解决方案4】:

      实际上,我已经搜索过它并找到了很多答案(其中大部分都在 SO 上),但没有一个对我有用。所以我终于找到了一个解决方案 - 根目录上的 .htaccess:

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

      这个来自http://framework.zend.com/wiki/display/ZFDEV/Configuring+Your+URL+Rewriter 和根目录上的 index.php:

      <?php 
      define('RUNNING_FROM_ROOT', true);
      include 'public/index.php';
      ?>
      

      这个来自http://akrabat.com/zend-framework/zend-framework-on-a-shared-host/ 即使根文件夹中有其他项目目录,它也对我有用——比如论坛左右。 认为它可能对某人有用:)

      【讨论】:

        【解决方案5】:

        ArneRie 的回答对我不起作用,我最终使用了Lorenzo Albertons solution。希望这有助于其他人获得更快的解决方案。

        RewriteEngine On
        
        RewriteRule ^\.htaccess$ - [F]
        
        RewriteCond %{REQUEST_URI} =""
        RewriteRule ^.*$ /public/index.php [NC,L]
        
        RewriteCond %{REQUEST_URI} !^/public/.*$
        RewriteRule ^(.*)$ /public/$1
        
        RewriteCond %{REQUEST_FILENAME} -f
        RewriteRule ^.*$ - [NC,L]
        
        RewriteRule ^public/.*$ /public/index.php [NC,L]
        

        【讨论】:

          【解决方案6】:

          您应该将项目的Public 文件夹放在主机中的www 文件夹中,这样您的地址就会变成yourdomain.com/。 您的源代码文件夹(例如库、应用程序...)应与 www 文件夹放在同一级别,而不是在其中。此方法将提高您网站的安全性

          【讨论】:

          • 这可能是最合适的答案。
          【解决方案7】:

          。 . . Magento 如何使用 Zend 框架???我使用的是 XAMPP,我确实在本地安装了 Magento,没有修改 php.ini、http.conf 或虚拟主机。

          【讨论】:

            【解决方案8】:

            在您的基本路径(即 /../public)下包含此 .htacces 文件:

            RewriteEngine On
            
            # Exclude some directories from URI rewriting
            #RewriteRule ^(dir1|dir2|dir3) - [L]
            
            RewriteRule ^\.htaccess$ - [F]
            
            RewriteCond %{REQUEST_URI} =""
            RewriteRule ^.*$ /public/index.php [NC,L]
            
            RewriteCond %{REQUEST_URI} !^/public/.*$
            RewriteRule ^(.*)$ /public/$1
            
            RewriteCond %{REQUEST_FILENAME} -f
            RewriteRule ^.*$ - [NC,L]
            
            RewriteRule ^public/.*$ /public/index.php [NC,L]
            

            并保留 publc 目录下的 .htaccess。

            因此您将拥有 2 个 .htaccess 文件,一个位于公共目录下(Zend Framework 文档中的普通文件),第二个位于您的基本路径下(我在上面发布的那个)。

            【讨论】:

            • 嗨 - 只是想知道你能发现我在这个帖子底部发布的细节有什么问题吗?
            • 我试过这个,但它没有找到其他目录,这些目录链接到公用文件夹之外的应用程序文件夹。其他简单的页面工作
            【解决方案9】:

            我也遇到过同样的情况,并将 zend 库放在 public 下的文件夹下,例如“src” - 并使用 .htaccess Deny from All。您必须兼顾几条路径,但效果很好。

            【讨论】:

              【解决方案10】:

              我建议最简单的方法:以 index.php 位于根目录的方式开发您的应用程序(更好 - 始终在 index.php 中定义 PUBLIC_PATH - dirname(__FILE__); 并建立与此常量相关的链接)。连同应用程序和库文件夹。您可以使用 .htaccess 中的 deny from all 轻松使它们无法从外部访问。

              PUBLIC_PATH 常量在您的公共不公开时也有帮助。 (公共 html 或 /public html/www(在我的情况下))

              【讨论】:

                【解决方案11】:

                我认为最好的方法是从公共目录(Zend Framework 目录结构)中删除 .htaccess,并将其与以下内容一起放入您的“根”目录:

                 
                RewriteEngine On
                
                

                RewriteRule ^.htaccess$ - [F] RewriteCond %{REQUEST_URI} ="" RewriteRule ^.*$ /public/index.php [NC,L]

                RewriteCond %{REQUEST_URI} !^/public/.$ RewriteRule ^(.)$ /public/$1 RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^.*$ - [NC,L]

                RewriteRule ^public/.*$ /public/index.php [NC,L]

                【讨论】:

                • 嗨 - 只是想知道你能发现我在这个帖子底部发布的细节有什么问题吗?
                • 可能的n00b问题:这对ZF路由有什么影响?也就是说,“/public”前缀不会是 ZF 看到的 url 的一部分(我猜是通过 $request 对象)吗?因此,现在是否所有 ZF 路由都需要“/public”感知?那将是非常不可取的,不是吗?
                • 环境设置放在哪里?
                • 这不适用于模块!如果我转到 admin 是模块的 www.domain.com/admin,我将被重定向到 www.domain.com/public/admin。请问如何解决这个问题?
                • 这非常有效。我没有时间发现正在发生的重定向,但在请求期间可能存在未满足的条件。使用 Zend 模块,这会在某些点导致无限循环。这可能需要更多的工作。
                【解决方案12】:

                确实,在共享主机上运行 Zend Framework 应用程序并不是最好的主意。我真的会推荐一个虚拟私人主机(VPS)。 Zend 框架和其他框架已经安装并定期更新,有非常好的和便宜的主机。我在servergrove,到目前为止一切都很好!

                但这并不意味着您不能让它在共享主机上运行。您只需要使用 .htacess 即可。将 public 文件夹的内容放入您的 webroot 并在 bootstrap.php 中调整您的路径,确保不能直接访问所有其他文件夹并使用通常的 ZF 方法通过您的 index.php 路由所有内容。

                【讨论】:

                  猜你喜欢
                  • 2012-02-16
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-09-11
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多