我怎样才能让 phalcon 框架在现有遗留 php 项目下共存?

how can I let phalcon framework co-exists under existing legacy php project?

我 运行 一个基于 php 的网站(购物中心)在 nginx 网络服务器上。 最近听说 phalcon 是一个非常快速可靠的框架,可以帮助开发人员加快开发过程。

所以我想将它实现到我的站点,作为特定服务的子模块开始,因为很难从头开始。

我想在 /var/www 下创建一个子文件夹(比方说 "phalcontest")。

我用谷歌搜索了这个话题,但是我在网上找到的所有(也许?)内容都是将 nginx 服务器设置为 phalcon 项目的专用站点(我的意思是,将 "the project" 文件夹作为一个 web root), 与使 phalcon 项目与旧的非 phalcon 页面共存无关。

我试过类似这样的 nginx 配置,希望没问题:

 server {
     listen       80;
     root /var/www/;
     index index.html index.htm index.php;

     server_name localhost;

     access_log  /var/log/nginx/access.log main buffer=16k;
     error_log  /var/log/nginx/error.log;


 location @rewrite {
     rewrite ^(.*)$ phalcontest/index.php?_url=;
 }

 location / { 
         try_files $uri $uri/ /index.php?q=$uri&$args;
 }

 location ~ \.php$ {
  try_files      $uri = 404;
  fastcgi_pass   127.0.0.1:9000;
  fastcgi_index  index.php;
  fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include        fastcgi_params;

  fastcgi_split_path_info  ^(.+\.php)(/.+)$;
  fastcgi_param PATH_INFO       $fastcgi_path_info;
  fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
 }


location /phalcontest {
     try_files $uri $uri/ @rewrite;
}

 location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
     root /var/www/;
 }

 }

使 "phalcontest" 文件夹成为 webroot 下启用 phalcon 的子文件夹。

之后我用 phalcon devtools 创建了 phalcontest,

访问http://localhost/phalcontest/public,显示:

Congratulations! You're now flying with Phalcon. Great things are about to happen! This page is located at views/index/index.phtml

但是当我访问 http://localhost/phalcontest 时,它显示:

Mod-Rewrite is not enabled

Please enable rewrite module on your web server to continue

这意味着我做错了什么,我只能通过 public 文件夹访问,而不是文件夹本身。

请帮助我如何让 phalcon 与现有的基于 php 的网站共存!

Phalcon 的工作方式应该是它将所有针对您的网络根目录 / 的流量重写到 /public/ 目录。这样做的要点是它使您能够从网络视图中隐藏 /app/ 目录,因为用户的请求将首先被重写为 /public/app/ 从而遇到 404 被重写为 /public/index.php 其中它会寻找控制器 "App"。如果你要从子目录 运行 Phalcon,你需要将所有 / 更改为 /phalcontest/.

我看到你正在使用 root /var/www/;
所以在你的情况下,你需要一个像这样的目录结构:

/www/phalcontest/public/index.php
/www/phalcontest/public/css/bootstrap.css
/www/phalcontest/public/js/jquery.js
/www/phalcontest/app/config/config.php
/www/phalcontest/app/controllers/IndexController.php
/www/phalcontest/app/models/
/www/phalcontest/app/views/index.phtml
/www/phalcontest/app/views/index/index.phtml

确保将所有针对 phalcontest(/.*)? 的请求重写为 phalcontest/public/,如果请求的文件或目录不存在,则从那里将它们重写为 index.php

这里有一个将 .htaccess 转换为 nginx 的工具,如果有帮助的话: http://winginx.com/en/htaccess

老实说,您不需要如此完美地遵循它们的目录结构。您只真正需要重写逻辑来将所有流量重写到您的 index.php 文件。关于 Phalcon 建议的 /public/ 目录的部分,如果您只是将 /app/ 目录移动到 Web 根目录下,则不需要 /public/ 目录,然后从您的 [=21] 中正确引用它的位置=] 文件.

Phalcon 在这里为您提供了很多关于如何为 nginx 设置重写的信息: https://docs.phalconphp.com/en/latest/reference/nginx.html

但是由于您是在本地主机上工作,我建议使用您的主机文件在本地主机上创建一个子域。然后您可以从 / 网络根目录工作并从 http://phalcontest.localhost/ 访问您的项目。然后你会在你的 nginx 配置中使用 server_name phalcontest.localhost; 而不是 server_name localhost; 并将它的根指向 root /var/www/phalcontest; 对于你的主机文件你会添加一行:phalcontest.localhost 127.0.0.1。这将使您免于 DNS 查找,因此子域会绑定到您的本地主机。参见 nginx subdomain configuration and How to test nginx subdomains on localhost