8

I am trying to use a router (AltoRouter) for the first time and am unable to call any page.

Web folder structure

enter image description here The Code

Index.php

require 'lib/AltoRouter.php';

$router = new AltoRouter();
$router->setBasePath('/alto');
$router->map('GET|POST','/', 'home#index', 'home');
$router->map('GET|POST','/', 'display.php', 'display');
$router->map('GET','/plan/', 'plan.php', 'plan');
$router->map('GET','/users/', array('c' => 'UserController', 'a' => 'ListAction'));
$router->map('GET','/users/[i:id]', 'users#show', 'users_show');
$router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do');
// match current request
$match = $router->match();

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

I have a file named plan.php (display plan) in the plan folder and the hyperlink that I am trying is

<a href="<?php echo $router->generate('plan'); ?>">Plan <?php echo $router->generate('plan'); ?></a>

which does not work.

Can you help?

1 Answers1

4

You cannot call plan.php by passing plan.php as an argument to match function

Check examples at http://altorouter.com/usage/processing-requests.html

If you want to use content from plan.php

you should use map in the following format

$router->map('GET','/plan/',  function() {
    require __DIR__ . '/plan/plan.php';
} , 'plan');

to the file plan/plan.php add echo 'testing plan';

Also, double check that your .htaccess file contains

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

Also, if you set base path with $router->setBasePath('/alto'); your index.php files should be placed in the alto directory so your url would be in that case http://example.com/alto/index.php

Working example:

require 'lib/AltoRouter.php';

$router = new AltoRouter();
$router->setBasePath('/alto');

$router->map('GET','/plan/',  function(  ) {
    require __DIR__ . '/plan/plan.php';
} , 'plan');

// match current request
$match = $router->match();

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

then this will work just fine

<a href="<?php echo $router->generate('plan'); ?>">Plan <?php echo $router->generate('plan'); ?></a>
Pawel Dubiel
  • 18,665
  • 3
  • 40
  • 58
  • I got the home page working but am unable to call plan.php. The home page has a link to plan Plan which I changed to Plan generate('plan'); ?>. –  Aug 05 '16 at 17:17
  • home#index this works because most likely you have class Home with method index in it. $router->map('GET','/plan/', function( ) { require __DIR__ . '/plan/plan.php'; } , 'plan'); When the route is matched this function above includes plan.php file. Alternative you may create class `plan` with method index and then you will be able to pass plan#index in the same way as for home page. – Pawel Dubiel Aug 09 '16 at 11:43