Routing through Php AltoRouter

I am trying to use a router (AltoRouter) for the first time and cannot 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 called plan.php (display plan) in the plan folder and the hyperlink I'm trying to do is

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

which does not work.

You can help?

+7
php routing routes router altorouter
source share
1 answer

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

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

If you want to use content from plan.php

you must use map in the following format

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

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

Also, check that your .htaccess file contains

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

Also, if you set the base path using $router->setBasePath('/alto'); , your index.php files must be placed in the alto directory so that your URL is in this 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 it will work great

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

All Articles