Altorouter can't execute routes

丶灬走出姿态 提交于 2019-12-08 06:09:41

问题


I am using Altorouter in a basic PHP App(No framework) but somehow it's not working. Below are details:

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";

It prints Done and no error in php log.

htaccess

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

I am access it as `http://localhost/home/myapp/


回答1:


Ok I figured out the issue. The URL I want to access is:

http://localhost/home/myapp/

Altorouter does not know about root URL so basePath needs to be set. it is done as:

$router->setBasePath('/home/myapp');

Do note that there's no trailing / should be put in setBasePath because we will put that in our map function like that:

$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';
}


来源:https://stackoverflow.com/questions/40253868/altorouter-cant-execute-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!