1

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

索引.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/的形式访问它

4

1 回答 1

2

好的,我发现了这个问题。我要访问的网址是:

http://localhost/home/myapp/

Altorouter不知道根 URL,因此需要设置 basePath。它是这样完成的:

$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';
}
于 2016-10-26T05:04:13.757 回答