php.htaccessaltorouter

Altorouter can't execute routes


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/


Solution

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