2016-10-26 71 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'導致所有這些問題? –