是否可以在没有课程的情况下路由uri?

时间:2012-12-27 07:40:51

标签: php .htaccess mod-rewrite pretty-urls

基本上标题是什么。我正在开发一个不涉及类的项目,我想实现htaccess和mod重写来创建漂亮的url。以下是我希望能够路由的一些示例。我的文件和目录结构也在下面列出。

/root
     - index.php (bootstrap)
     /template
     /sources
          /forums
               -threads.source.php
               -discussions.source.php
               -post.source.php
               -index.source.php
          //
          - members.source.php (functions for register, login, etc)
          - index.source.php

http://localhost/myapp/members/register -> sources/members.source.php register function
http://localhost/myapp/ -> sources/index.source.php
http://localhost/myapp/forums/threads/Some-Cool-Thread/0001 -> sources/forums/threads.source.php view function
http://localhost/myapp/forums/ ->sources/forums/index.source.php

我希望这对你有意义。顺便说一句:我使用.source.php作为文件名,因为我还将使用模板文件,例如,members.template.php

编辑:我已经创建了我的.htaccess文件,并且还在我的索引文件中获得了URI字符串。我有点验证uri段字符。这是我的.htaccess文件:

Options +FollowSymLinks  
    RewriteEngine On  
    RewriteCond %{SCRIPT_FILENAME} !-d  
    RewriteCond %{SCRIPT_FILENAME} !-f  
    RewriteRule ^(.*?) index.php

Options +FollowSymLinks RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^(.*?) index.php

这是我的索引文件:

<?php

session_start();

require_once('config.php');

global $config;

date_default_timezone_set($config['server']['default_timezone']);

define('DOC_ROOT', dirname(__FILE__));

if(!mysql_connect($config['db']['server'], $config['db']['user'], $config['db']['pass'])){

    die('System Failure:  Could not connect to the requested database server.  It may be down or it may be too busy!  Please check back later.');

} else if(!mysql_select_db($config['db']['name'])){

    die('System Failure:  Could not connect to the requested database server.  It may be down or it may be too busy!  Please check back later.');

} else {

    $uri_segments = $_SERVER['REQUEST_URI'];

    $uri_segments = str_replace('/Base%20Command/', '', $uri_segments);
    $uri_segments = explode('/', $uri_segments);


    foreach($uri_segments as $segment){

        if(!preg_match('^[a-z0-9_-]^', $segment)){

            die('Could not accept provied URI string... the string contains invalid characters.');

        }

    }

    if(file_exists(DOC_ROOT.'/sources/global.source.php')){

        require_once(DOC_ROOT.'/sources/global.source.php');

    }

    if(file_exists(DOC_ROOT.'/template/global.template.php')){

        require_once(DOC_ROOT.'/template/global.template.php');

        if(function_exists('html_header')){

            $output = html_header();

            if(file_exists(DOC_ROOT.'/template/'.$source.'.template.php')){

                require_once(DOC_ROOT.'/template/'.$source.'.template.php');

                if(function_exists($action)){

                    $output .= $action();

                } else {

                    $output .= Error404();

                }

            } else {

                $output .= Error404();

            }

            if(function_exists('html_footer')){

                $output .= html_footer();

            }

        } else {

            $output = 'System Failure:  Certain template files did not load correctly.';

        }

        echo $output;

    }

}

?>

基本上我需要做的是找到一种方法来有效地为源和动作变量提供值。

0 个答案:

没有答案