2017-08-13 51 views
-1

沒有找到類這是我在MVC項目基於請求的URL

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-l 
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L] 

的.htaccess這是我在控制器文件夾的簡單類

class index { 
    function __construct() 
    { 
     echo "this page is index"; 
    } 
} 

,這是我的索引頁,我想,當在url中的控制器部分是索引它打印控制器部分和運行類。但它的錯誤:

Undefined index: url  

require(controllers/.php): failed to open stream: No such file or directory 

我的代碼:

$url = $_GET['url']; 
$url = rtrim($url,'/'); 
$url = explode('/',$url);//breaks string to array 
require 'controllers/'.$url[0].'.php'; 
$controller = new index(); 
print_r($url); 
+0

閱讀了約'spl_autoload_register()'和PSR4。而且,這些都不是與MVC切線相關的。 –

+0

您收到的錯誤(實際上只是一個警告)表示您發佈的重寫規則未得到應用。也許你有其他適用於以前的規則? – arkascha

回答

0
  • 不要忘記重寫了 「基地」 的重定向(RewriteBase)。
  • 刪除行RewriteCond %{REQUEST_FILENAME} !-l-l 選項是否存在?

這裏是與文檔根 「公共」,其中文件 「的index.php」 駐留在例如:

<Directory "[your-absolute-path-to]/Publics"> 
    Options FollowSymLinks 

    # Uncomment if you want to only allow localhost. 
    # Allow from 127.0.0.1 

    # Activate rewriting engine. 
    RewriteEngine On 

    # Allow pin-pointing to "index.php" using RewriteRule. 
    RewriteBase/

    # Rewrite url only if no physical folder name is given in url. 
    RewriteCond %{REQUEST_FILENAME} !-d 

    # Rewrite url only if no physical file name is given in url. 
    RewriteCond %{REQUEST_FILENAME} !-f 

    # Take the common url and parse it through "index.php" as a query string, e.g as "url=[...]". 
    # ---------------------------------------------------------------------------------- 
    # SEE: RewriteRule Flags >> https://httpd.apache.org/docs/current/rewrite/flags.html 
    # ---------------------------------------------------------------------------------- 
    RewriteRule ^(.+)$ index.php?url=$1 [QSA,L,B] 
</Directory>