4

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

索引.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');
}
?>

.htaccess

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

问题:当我访问 localhost 时,'home.php' 得到了服务,但是当我访问 'localhost/login' 或 'localhost/signup' 时,我得到 404 错误。

4

4 回答 4

2

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

于 2016-08-30T21:15:00.333 回答
2

更改此代码

 $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');
于 2016-08-30T21:44:03.170 回答
1

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

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

于 2016-10-31T10:36:15.387 回答
0

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

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

就是这样。

于 2017-05-03T04:54:27.663 回答