2017-08-13 58 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>