首页 文章

Phalcon,IIS 7.5和URL重写问题

提问于
浏览
2

所以,我在这个链接http://docs.phalconphp.com/en/latest/reference/tutorial.html上关注Phalcon php教程,但我'm trying to recreate the process using IIS 7.5 (as opposed to apache) and I' m有以下添加/错误行为 .

NOTE: The server name and port is something I created locally via IIS 7.5 so it won't do anything meaningful if you click on it or copy and paste into the browser address bar.

SCENARIO 1:
Browser Address Bar Input: http://Phalcon:8181/
Browser Address Bar Result: http://Phalcon:8181/public/public/public/public/public/...[ad_nauseaum]../public/ (不是预期的结果 . 它应该是 http://Phalcon:8181/index/index ,因为它们在"bootstrap" index.php文件中被设置为默认值)
Browser Page Result: 预期页面(/public/index.php然后路由到/public/controllers/index.php并默认使用操作"index")

SCENARIO 2:
Browser Address Bar Input: http://Phalcon:8181/index.php
Browser Address Bar Result: http://Phalcon:8181/index.php (不是预期的结果 . 它应该是 http://Phalcon:8181/index/index ,因为它们被设置为"bootstrap" index.php文件中的默认值)
Browser Page Result: 预期页面(/public/index.php路由到正确的控制器和操作)

SCENARIO 3:
Browser Address Bar Input:
Action By User: 点击Phalcon生成的链接并指向"singup/index"
Browser Address Result: http://Phalcon:8181/public/signup/index (不是预期的结果,我期待 http://Phalcon:8181/signup/index ,因为它们都是在Phalcon生成的链接中被激发的)

我已经尝试搞乱web.configs或bootstrap php,但我做的每一个其他更改都会导致IIS中找不到404页错误

基于他们的“创建项目 - >文件结构”部分,我创建了我的站点文件夹结构,如下所示:

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

文件夹“tutorial”是我在IIS 7.5中创建的网站的根目录

在“Beautiful URL”部分中,它描述了如何重定向请求,以便它们与Phalcon提供的MVC模式一起使用 .

So this rewrite mod

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

Converts to this for IIS as is placed just inside the "tutorial" folder in a web.config file:

<?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>

And this rewrite mod

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

Converts to this for IIS and is placed just inside the "tutorial/public" folder in a web.config file:

<?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>

As for the PHP bootstrap file that is supposed to handle the routing, it has the code below (and it's located just inside the "public" folder as 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();
    }

?>

关于如何无缝地工作的任何想法?

1 回答

  • 0
    <?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 而不是两个 .

    Note: <projectpath> 不是变量!
    我找不到一种优雅的方式来引用当前路径,因此虽然 C:\inetpub\wwwroot 可以写成 {DOCUMENT_ROOT} ,但仍需要明确输入 <projectpath> (例如 projects/my_phalcon_project ) .

    Comment:
    web.config 确实非常强大,但我发现它比较简洁_1114759_等价物更容易掌握 . 没有先在线阅读documentation and examples,我不能只带你的并修改它 . 还涉及试验和错误,因为某些行为既没有记录也没有直观 .


    与上面的类似 web.config ,我能够在IIS 8.5上完成Phalcon tutorial . 但是,它首先定义 $url->setBaseUri('/'); 的说明中似乎存在不一致,然后显示访问 localhost/tutorial/ 的浏览器的屏幕截图 . 显然,对于可在 localhost/tutorial/ 处到达的项目,它应为 $url->setBaseUri('/tutorial/'); .

相关问题