我是 Zend 框架的新手。我想知道如何在共享主机上实现 zend 框架。由于 zend 框架文件夹结构,所有视图文件都放入“公共”文件夹。
认为
“/”是我的主根文件夹,公共就像“/public”
这样 url 就变成了“ http://site/public/ .. .bla bla ...”
它是否正确?
还是有其他方法?
我没有创建虚拟主机的任何权限。
那么该怎么办?
我希望你能理解我的问题。如果没有,请问我。
谢谢!
我是 Zend 框架的新手。我想知道如何在共享主机上实现 zend 框架。由于 zend 框架文件夹结构,所有视图文件都放入“公共”文件夹。
认为
“/”是我的主根文件夹,公共就像“/public”
这样 url 就变成了“ http://site/public/ .. .bla bla ...”
它是否正确?
还是有其他方法?
我没有创建虚拟主机的任何权限。
那么该怎么办?
我希望你能理解我的问题。如果没有,请问我。
谢谢!
我认为最好的方法是从公共目录(Zend 框架目录结构)中删除 .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]
在您的基本路径(即 /../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 文档中的普通文件),另一个位于您的基本路径下(我在上面发布的那个)。
您应该将项目的 Public 文件夹放在主机中的 www 文件夹中,这样您的地址将变为 yourdomain.com/。您的源代码文件夹(例如库、应用程序...)应与 www 文件夹放在同一级别,而不是在其中。此方法将提高您网站的安全性
事实上,在共享主机上运行 Zend Framework 应用程序并不是最好的主意。我真的会推荐一个虚拟私人主机(VPS)。Zend 框架和其他框架已经安装并定期更新,有非常好的和便宜的主机。我在servergrove上,到目前为止一切都很好!
但这并不意味着您不能让它在共享主机上运行。您只需要使用 .htacess 即可。将 public 文件夹的内容放入您的 webroot 并在 bootstrap.php 中调整您的路径,确保所有其他文件夹不能直接访问,并使用通常的 ZF 方法通过您的 index.php 路由所有内容。
ArneRie 的回答对我不起作用,我最终使用Lorenzo Albertons 解决方案。希望这有助于其他人获得更快的解决方案。
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]
实际上我已经搜索过它并找到了很多答案(其中大部分都在这里),但没有一个对我有用。所以我终于找到了一个解决方案 - 根目录上的 .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/ 它对我有用,即使根文件夹中有其他项目目录 - 比如论坛左右。认为它可能对某人有用:)
Rob Allen也写了一个很好的解决方案
我正在使用Zend Framework 2,它对我有用。虽然我还没有从上面的链接中完成部分“参考面向公众的文件” 。
我刚刚更改了layout.phtml中的代码:
$basePath = $this->basePath();
到:
$basePath = $this->basePath();
if (defined('RUNNING_FROM_ROOT')) {
$basePath .= 'public/';
}
我一直处于同样的情况,并将 zend 库放在 public 下的文件夹下,如“src” - 并使用 .htaccess Deny from All。你必须兼顾几条路径,但它工作正常。
. . . Magento 如何使用 Zend 框架???我正在使用 XAMPP,我确实在本地安装了 Magento,没有修改 php.ini、http.conf 或虚拟主机。
我最近遇到了这个问题,通过这个线程我能够找到解决方案。我正在使用zf2。所以我所要做的就是将与 zend 相关的文件放在一个我们可以命名的目录中src
。该目录将驻留在公共目录或根目录中(例如/www
)
在 'src' 目录中创建一个 .htaccess 文件并添加 Deny from All 指令。
src
在索引文件中,通过执行以下操作将工作目录更改为文件夹:
define('SRC_ROOT', 'src');
chdir(SRC_ROOT);
......你很高兴去!
设置虚拟主机通常在 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。
我建议最简单的方法:以 index.php 位于您的根目录的方式开发您的应用程序(更好 - 始终定义 PUBLIC_PATH -dirname(__FILE__);
在您的 index.php 中并建立与此常量相关的链接)。连同应用程序和库文件夹。deny from all
您可以使用.htaccess轻松使它们无法从外部访问。
当您的公共不公开时,PUBLIC_PATH 常量也有帮助。(公共 html 或 /public html/www(在我的情况下))