PHP AltoRouter只提供基本URL

PHP AltoRouter只提供基本URL,php,apache,.htaccess,routing,altorouter,Php,Apache,.htaccess,Routing,Altorouter,我是PHP中的一个noobie,我正在使用AltoRouter设置一个简单的路由。下面是我的index.php和.htaccess文件,它们位于route文件夹,即/var/www/html/ 我使用Apache2为网页提供服务 index.php <?php require 'vendor/AltoRouter.php'; $router = new AltoRouter(); // map homepage $router->map('GET', '/', function (

我是PHP中的一个noobie,我正在使用AltoRouter设置一个简单的路由。下面是我的index.php和.htaccess文件,它们位于route文件夹,即/var/www/html/ 我使用Apache2为网页提供服务

index.php

<?php
require 'vendor/AltoRouter.php';

$router = new AltoRouter();
// map homepage
$router->map('GET', '/', function () {
    require __DIR__ . '/views/home.php';
});

$router->map('GET|POST', '/login', function () {
    require __DIR__ . '/views/login.php';
});

$router->map('GET', '/signup', function () {
    require __DIR__ . '/views/signup.php';
});

$match = $router->match();

// call closure or throw 404 status
if ($match && is_callable($match['target'])) {
    call_user_func_array($match['target'], $match['params']);
} else {
    // no route was matched
    header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
}
?>
问题:
当我访问localhost时,“home.php”会得到服务,但当我访问“localhost/login”或“localhost/signup”时,会得到404错误

我希望您应该访问
localhost/login.php
。请尝试一下。

更改此代码

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

 $router->map('GET|POST', '/login', function () {
     require __DIR__ . '/views/login.php'; });

 $router->map('GET', '/signup', function () {
     require __DIR__ . '/views/signup.php'; });
以下是

$router->map('GET', '/views', '/views/home.php','home');

$router->map('GET', '/views/login', '/views/login.php','login');

$router->map('GET', '/views/signup', '/views/signup.php','signup');

似乎您的.htaccess文件被忽略了,这不是由AltoRouter或PHP引起的


根据您拥有的操作系统,您应该搜索如何激活系统上的.htaccess文件。

我也遇到过同样的问题,您可以通过向路由器添加基本路径来解决。假设我在目录
http://localhost/myapp/
和我的公共目录在我有
index.php
文件
http://localhost/myapp/public/index.php
基本路径应为

$router->setBasePath('/myapp/public');
                                   ^----don't but any / here

就这样。

不,没用。在这里发布问题之前,我尝试了链接中的所有内容,不确定问题到底出在哪里。
$router->setBasePath('/myapp/public');
                                   ^----don't but any / here