2015-04-17 26 views
2

我在我的PHP項目中使用Klein.php進行路由。我的項目目錄具有以下結構:使用Klein-PHP來過濾請求

myProject 
| 
*---Restricted-- 
|    | 
|    *---index.php 
|    | 
|    *---blah.html 
|    | 
|    *---some_directory 
| 
| 
*---some_other_directories 
| 
*---index.html 
| 
*---fun.html 
| 
*---css 
| 
*---js 

我想是處理所有請求,只Restricted文件夾。對於其他任何事情,我只是希望服務器完成其工作,即只需提供其餘文件。

我該如何做到這一點?

編輯

我有它配置爲通過CORY在回答給:

// .htaccess file 

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php [QSA,L] 

// index.php 

<?php 

    require_once __DIR__ . '/vendor/autoload.php'; 

    $klein = new \Klein\Klein(); 

    $klein->respond('@^/Restricted/', function() { 
     // checking if authenticated user 
    }); 

    $klein->dispatch(); 

?> 
+0

如何你現在有配置嗎? –

+0

@Cᴏʀʏ請求說,'blah.html'應該被轉發到index.php,但對於像'fun.html'這樣的文件,它應該被簡單地提供。現在只有那些在index.php中定義了一些匹配規則的鏈接才能工作,其餘的我會看到一個空白頁面。 – asaini

回答

0

權的Github page for Klein.php有一個在路由這個小例子部分:

// Match all requests that _don't_ start with /admin 
$klein->respond('[email protected]^/admin/', ... 

我想你基本上要在相反(匹配開始與/restricted所有請求),所以讓我們去掉!(否定),並替換正確的文件夾名稱:

// Match all requests that start with /Restricted 
$klein->respond('@^/Restricted/', ...