Phalcon、IIS 7.5和URL重写问题


Phalcon, IIS 7.5 and URL Rewrite problems

所以,我在这个链接上学习Phalcon-php教程http://docs.phalconphp.com/en/latest/reference/tutorial.html,但我正在尝试使用IIS 7.5(与apache相反)重新创建该进程,并且我有以下添加/错误行为。

注意:服务器名称和端口是我通过IIS 7.5在本地创建的,因此如果您单击它或复制并粘贴到浏览器地址栏中,它将不会有任何意义

场景1:
浏览器地址栏输入:http://Phalcon:8181/
浏览器地址栏结果:http://Phalcon:8181/public/public/public/public/public/...[ad_nauseaum]../public/(不是预期结果。应该是http://Phalcon:8181/index/index,因为它们在"bootstrap"index.php文件中被设置为默认值)
浏览器页面结果:预期页面(/public/index.php,然后路由到/public/controllers/index.php并默认使用操作"index")

场景2:
浏览器地址栏输入:http://Phalcon:8181/index.php
浏览器地址栏结果:http://Phalcon:8181/index.php(不是预期结果。应该是http://Phalcon:8181/index/index,因为它们在"bootstrap"index.php文件中被设置为默认值)
浏览器页面结果:预期页面(/public/index.php,路由到正确的控制器和操作)

场景3:
浏览器地址栏输入:
用户操作:单击Phalcon生成的链接并指向"singup/index"
浏览器地址结果:http://Phalcon:8181/public/signup/index(不是预期结果,我预期的是http://Phalcon:8181/signup/index,因为它们都在Phalcon生成的链接中提供)

我试过破坏web.config或bootstrap php,但我所做的每一次更改都会导致IIS 出现404页未找到错误

基于他们的"创建项目->文件结构"部分,我创建了我的网站文件夹结构如下:

tutorial/
  app/
    controllers/
    models/
    views/
  public/
    css/
    img/
    js/

文件夹"教程"是我在IIS 7.5 中创建的网站的根目录

在"美丽的URL"一节中,它描述了如何重定向请求,以便它们使用Phalcon提供的MVC模式。

所以这个重写模式

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule  ^$ public/    [L]
    RewriteRule  (.*) public/$1 [L]
</IfModule>  

对于IIS,将其转换为web.config文件中"教程"文件夹中的内容:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Imported Rule 1" stopProcessing="true">
                    <match url="^$" ignoreCase="false" />
                    <action type="Rewrite" url="public/" />
                </rule>
                <rule name="Imported Rule 2" stopProcessing="true">
                    <match url="(.*)" ignoreCase="false" />
                    <action type="Rewrite" url="public/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
    <location path="public">
    </location>
</configuration>  

这个重写模式

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>  

对于IIS转换为this,并放置在web.config文件中的"tutorial/public"文件夹中:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Imported Rule 3" enabled="true" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                    </conditions>
                    <serverVariables />
                    <action type="Rewrite" url="index.php?_url=/{R:1}" appendQueryString="true"    />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>  

至于应该处理路由的PHP引导文件,它有下面的代码(它位于"public"文件夹中的index.PHP:

<?php
    try{
        // create a router
        $router = new 'Phalcon'Mvc'Router();
        $router->setDefaultController("index");
        $router->setDefaultAction("index");
        $router->add("/public", array(
                "controller" => "index",
                "action" => "index"
            ));
        $router->notFound(array(
                "controller" => "index",
                "action" => "route404"
            ));
        // Register an autoloader
        $loader = new 'Phalcon'Loader();
        $loader->registerDirs(array(
                '../app/controllers/',
                '../app/models/'
            ))->register();
        // Create a DI
        $di = new 'Phalcon'DI'FactoryDefault();
        // Setup the view component
        $di->set('view', function(){
            $view = new 'Phalcon'Mvc'View();
            $view->setViewsDir('../app/views/');
            return $view;
        });
        // Setup a basic URI so that all generated URIs includ the tutorial folder
        $di->set('url', function(){
            $url = new 'Phalcon'Mvc'Url();
            $url->setBaseUri('/public/');
            return $url;
        });
        // Handle the request
        $application = new 'Phalcon'Mvc'Application($di);
        echo $application->handle()->getContent();
    } 
    catch ('Phalcon'Exception $e)
    {
        echo "PhalconException: " , $e->getMessage();
    }
?>

有没有想过如何让这项工作无缝衔接?

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Imported Rule 1" stopProcessing="true">
                    <match url="^$" ignoreCase="false" />
                    <action type="Rewrite" url="public/" />
                </rule>
                <rule name="Imported Rule 2" stopProcessing="true">
                    <match url="^(public/)?([^/]+)$" ignoreCase="false" />
                    <conditions>
                        <add input="C:'inetpub'wwwroot'<projectpath>'public'{R:2}" matchType="IsFile" />
                    </conditions>
                    <action type="Rewrite" url="public/{R:2}" appendQueryString="true" />
                </rule>
                <rule name="Imported Rule 3" stopProcessing="true">
                    <match url="(.*)" ignoreCase="false" />
                    <action type="Rewrite" url="public/index.php?_url={R:1}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

放在项目根目录中,这将复制phalcon create-project <projectname>生成的两个.htaccess文件的效果。只需要一个web.config,而不需要两个。

注意:<projectpath>不是变量
我找不到一种优雅的方式来引用当前路径,所以虽然C:'inetpub'wwwroot可以写成{DOCUMENT_ROOT},但<projectpath>仍然需要显式键入(例如projects/my_phalcon_project)。

评论:
web.config确实相当强大,但我发现它可能比更简洁的.htaccess更难理解。如果不先阅读在线文档和示例,我就不能只拿你的来修改它。由于一些行为既没有记录也没有直觉,因此也涉及试错。


使用与上面类似的web.config,我能够完成IIS 8.5上的Phalcon教程。然而,指令中似乎存在不一致之处,它首先定义了$url->setBaseUri('/');,然后显示了访问localhost/tutorial/的浏览器的屏幕截图。显然,对于在localhost/tutorial/上可访问的项目,它应该是$url->setBaseUri('/tutorial/');