user2379271
user2379271

Reputation: 631

PHP AltoRouter serving only base URL

I am a noobie in PHP, I am setting up a simple Routing using AltoRouter. Below is my index.php and .htaccess file which are at the route folder i.e, /var/www/html/ I am using Apache2 for serving the web pages.

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]

Problem: When I visit localhost, the 'home.php' get served, but when I visit 'localhost/login' or 'localhost/signup', I get 404 error.

Upvotes: 4

Views: 1652

Answers (4)

Abdalla Arbab
Abdalla Arbab

Reputation: 1400

I have faced the same problem and you can solve it by adding a base_path to the router . Lets say I have my altoroute project in the directory http://localhost/myapp/ and my public directory in I have index.php file http://localhost/myapp/public/index.php the base path should be

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

And that's it.

Upvotes: 0

Sheki
Sheki

Reputation: 1635

It seem that your .htaccess file is being ignored, that's not caused by AltoRouter or PHP.

Depending on which operating system you have, you should search how to activate the .htaccess file on your system.

Upvotes: 1

ashik
ashik

Reputation: 119

Change this code

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

with the following.....

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

Upvotes: 2

ashik
ashik

Reputation: 119

I hope you should visit localhost/login.php .try it please.

Upvotes: 2

Related Questions