Можно ли маршрутизировать URI без классов?

В основном то, что говорит название. Я работаю над проектом, который не включает в себя классы, и я хотел бы реализовать htaccess и mod rewrite для создания красивых 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

Вот мой индексный файл:

<?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 ответов

Другие вопросы по тегам