2016-08-30 46 views
3

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

的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'); 
} 
?> 

的.htaccess

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

问题: 当我访问本地主机中, 'home.php' 得到服务,但是当我访问 '本地主机/登录' 或'本地主机/注册',我得到404错误。

回答

1

我希望你应该访问localhost/login.php.try它请。

+0

我得到相同的404错误。 – user2379271

1

改变此密码

$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'); 
+0

不,不帮忙。还在404 – user2379271

+0

请访问链接https://www.longren.io/basic-routing-in-php-with-altorouter/ – ashik

+0

我在这里发布问题之前尝试了所有链接,不确定问题出在哪里。 – user2379271

1

这似乎是你的.htaccess文件被忽略,这不是由AltoRouter或PHP造成的。

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

0

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

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

就是这样。

+0

http://stackoverflow.com/questions/40253868/altorouter-cant-execute-routes –