3

For some reason I am not able to start AltoRouter. I am trying the most basic call, but nothing is happening. How can I make it work? My index.php file looks like this:

    <?php

    include('settings/autoload.php');

    use app\AltoRouter;

    $router = new AltoRouter;

    $router->map('GET', '/', function(){

        echo 'It is working';
    });

$match = $router->match();

autoload.php:

<?php

require_once('app/Router.php');
Paolo
  • 20,112
  • 21
  • 72
  • 113
Sasha
  • 8,521
  • 23
  • 91
  • 174
  • Any error message? Blank page? what's happening in your index.php? – Federico J. Jan 26 '15 at 15:14
  • There are no errors and the page is blank. – Sasha Jan 26 '15 at 15:15
  • Show your errors with this: http://stackoverflow.com/questions/5911964/cannot-get-php-display-errors-enabled – Federico J. Jan 26 '15 at 15:19
  • Still 0 errors (I am running php on linux, and any error is showing in the terminal); – Sasha Jan 26 '15 at 15:21
  • did you try to get it via browser? just to be sure you're getting something – Federico J. Jan 26 '15 at 23:48
  • hey @Sasha, any news on this? I'm getting the same problem and can't get fixed. – Paolo Sep 15 '15 at 22:18
  • @Paolo which PHP-Version are you using? I think it's an syntax error because some Language constructs aren't supported in older PHP versions. In this case, error Reporting may not be enabled. – tillz Sep 16 '15 at 00:18
  • This is the package you're using, right? https://github.com/dannyvankooten/AltoRouter. If so, the class in that repository is not under any namespace. Then, why are you using `app/AltoRouter`? – Gustavo Straube Sep 16 '15 at 01:28
  • My issue details: https://github.com/dannyvankooten/AltoRouter/issues/124 – Paolo Sep 16 '15 at 05:59
  • 1
    Check the link http://altorouter.com/usage/processing-requests.html. According to it, AltoRouter would not process the request. Add the piece of code after `$router->match();` and then it will work. – VolenD Sep 16 '15 at 11:07

1 Answers1

11

Your Problem is that AltoRouter, according to the documentation (and in contrast to the Slim Framework, which seems to have the the same syntax), won't process the request for you, it only matches them. So by calling $router->match() you get all the required information to process the request in any way you like. If you just want to call the closure-function, simply modify your code:

<?php

// include AltoRouter in one of the many ways (Autoloader, composer, directly, whatever)
$router = new AltoRouter();

$router->map('GET', '/', function(){

    echo 'It is working';
});

$match = $router->match();

// Here comes the new part, taken straight from the docs:

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

And voilà - now you'll get your desired output!

tillz
  • 2,108
  • 15
  • 21