0

独自のテンプレート システムを備えた PHP フレームワークを作成しました。これは、フロントエンドで必要に応じて機能します。テンプレート システムは?url=($page)、URL の の部分を使用して、ユーザーが要求するページを特定し、ファイルの内容を という名前で表示します($page)。次に、htaccess を使用して URL を「きれいに」します。

ただし、フレームワークを拡張して、$_GET['url']containsかどうかを識別し、含まれbackendている場合は、別の文字列の末尾にスラッシュがあるかどうかを確認します。たとえば、の値が$_GET['url']isの場合、バックエンド フォルダー内からbackend/manage-galleryページを返すようにします。manage-galleryここに私が今持っているコードがあります。$_GET['url']現在、値が backendの場合、ページを静的に取得するようになりました (index.php) 。

public function addTpl() {     
    global $zip;

    if(!isset($_GET['url']) || empty($_GET['url'])) {
        $_GET['url'] = 'index';
    }

    $front = '_zip/_templates/_front/'. $zip['Template']['Front'];
    $back  = '_zip/_templates/_back/'. $zip['Template']['Back'];

    if(strpos($_GET['url'], 'backend') !== false) {
       if(file_exists($back . '/')) {
            if(file_exists($back . '/index.php')) {
                ob_start();
                include($back . '/index.php');
                $this->tpl .= ob_get_contents();
                ob_end_clean();
            } else {
                if($zip['Template']['Front'] == 'Refresh/multi-page') {
                    ob_start();
                    include($back . '/404.php');
                    $this->tpl .= ob_get_contents();
                    ob_end_clean();
                } else {
                    die(zipError('File Not Found', 'The file <b>' . secure($_GET['url']) . '</b> could not be found. Please re-check the URL; If you were directed here using a link, please report that link <b><a href="mailto:IntactDev@gmail.com">here<a/>.</b>'));
                }
            }
        } else {
            die(zipError('Template Not Found', 'The template <b>' . $zip['Template']['Front'] . '</b> could not be found. Please check your configuration file for a mistake.'));
        } 
    } else {
        if(file_exists($front. '/')) {
            if(file_exists($front. '/' . secure($_GET['url']) . '.php')) {
                ob_start();
                include($front. '/' . secure($_GET['url']) . '.php');
                $this->tpl .= ob_get_contents();
                ob_end_clean();
            } else {
                if($zip['Template']['Front'] == 'Refresh/multi-page') {
                    ob_start();
                    include($front. '/404.php');
                    $this->tpl .= ob_get_contents();
                    ob_end_clean();
                } else {
                    die(zipError('File Not Found', 'The file <b>' . secure($_GET['url']) . '</b> could not be found. Please re-check the URL; If you were directed here using a link, please report that link <b><a href="mailto:IntactDev@gmail.com">here<a/>.</b>'));
                }
            }
        } else {
            die(zipError('Template Not Found', 'The template <b>' . $zip['Template']['Front'] . '</b> could not be found. Please check your configuration file for a mistake.'));
        }
    }
}

get 値に backend が含まれているかどうかをコードで確認し、その後に文字列が含まれるスラッシュがあるかどうかを確認するにはどうすればよいですか? ここに私のhtaccessがあります

RewriteEngine On
RewriteRule ^(|/)$ index.php?url=$1
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ index.php?url=$1
4

1 に答える 1

1

backendスラッシュは常に文字列の直後にあるため、スラッシュをstrpos追加して変更します。

if(strpos($_GET['url'], 'backend/') !== false) {

また、backend文字列で発生した場合、常に文字列の先頭にあるため、位置ゼロで検索します。

if(strpos($_GET['url'], 'backend/') == 0) {

次に、&&そのステートメントに別の条件を使用して追加しif、文字列が 7 文字を超えているかどうかを確認します。つまり、backend/が先頭にあり、文字列が 7 文字より長い場合、 の後にさらに文字がありますbackend/

if(strpos($_GET['url'], 'backend/') == 0 && strlen($_GET['url']) > 7) {

編集:.htaccessファイルに問題があります。に変更$1$2ます。

于 2014-11-24T22:45:52.627 に答える