PHP5.4+內置webserver,Yii中如何隱藏index.php,讓URL rewrite

Yii 中爲了美化URL,能夠把php

/index.php?r=post/view&id=100

轉變成下面path的形式:html

/index.php/post/100

Yii的配置中加入:nginx

<?php
        'urlManager'=>array(
            'urlFormat'=>'path',
//            'urlFormat'=>'get',
            'urlSuffix' => ".do",
            // Disable index.php
            'showScriptName' => false,
            // Disable r= routes
            'useStrictParsing' => true,
            // 'urlFormat'=> isset($_GET['sdkVersion']) && ($_GET['sdkVersion'] > '1.0.0') ? 'get' : 'path',
            'rules'=>array(
                '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
            ),
        ),

詳細說明可參考官方文檔:
http://www.yiiframework.com/doc-2.0/guide-runtime-routing.htmlweb

美化後的路徑仍然包含index.php,不夠優雅,因此須要去掉,apache 和 nginx中都有rewrite的實現,網上不少,再也不重複。apache

爲了調試便捷,好比本人,基本不在開發機上部署apache、nginx這些。而是直接用PHP 5.4加入的內建Web Server,啓動命令:app

php -S 127.0.0.1:80

要隱藏index.php 這個,就須要用到 URL rewrite了,PHP內建webserver沒這個功能,卻也給咱們提供了技巧,PHP官方參考:
http://php.net/manual/en/features.commandline.webserver.phpyii

好比個人Yii在appcall子目錄中,因此在appcall下面建立 route.php,內容以下:ide

cat appcall/route.php
<?php
if (strpos($_SERVER['REQUEST_URI'], '/appcall/') === false) {
    return false;
} else {
    include __DIR__ . '/index.php';
}

注意啓動命令後面要加上這個 route.php:post

php -S 127.0.0.1:80 appcall/route.php

OK,這樣就知足咱們想要的了。ui

轉自:http://www.yinqisen.cn/blog-694.html

相關文章
相關標籤/搜索