2016-10-26 55 views
1

我在一个基本的PHP应用(无框架)中使用Altorouter,但不知何故它不起作用。以下是详细信息:Altorouter不能执行路由

的index.php

<?php 
error_reporting(E_ALL); 
ini_set('display_errors',1); 
require_once __DIR__ . '/vendor/autoload.php'; 

$router = new AltoRouter(); 

$router->map('GET', '/', function() { 
    include __DIR__ . 'home.php'; 
}); 

print "Done"; 

它打印完成和PHP的日志中没有错误。

htaccess的

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule . index.php [L] 

我访问它作为'http://localhost/home/myapp/

+1

也许应该是'包括__DIR__ 。 '/home.php';' – Phil

+0

@菲尔仍然没有工作。 – Volatil3

+0

@Phil - 是的,我甚至会说使用'require'来代替。包含我发现的唯一真正的好处是,如果您返回像包含文件中的数组。否则,要求会失败并告诉你缺少一个斜杠becase __DIR__不会将结尾斜杠添加到路径。 – ArtisticPhoenix

回答

2

好吧,我想通了这个问题。我想要访问的网址是:

http://localhost/home/myapp/

Altorouter不知道根URL,这样基本路径需要设置。它完成如下:

$router->setBasePath('/home/myapp');

请注意,有没有尾随/应放在setBasePath,因为我们将会把在我们map功能类似:

$router->map('GET', '/', 'home.php', 'home'); 
$match = $router->match(); 
if ($match) { 
    require $match['target']; 
} else { 
    header("HTTP/1.0 404 Not Found"); 
    require '404.html'; 
} 
+0

所以'setBasePath'导致所有这些问题? –