This is the index.php
<?php
include 'library/altorouter.php';
$router = new AltoRouter();
$router->setBasePath('/AltoRouter');
$router->map('GET','/', 'home_controller#index', 'home');
$router->map('GET','/content/[:parent]/?[:child]?', 'content_controller#display_item', 'content');
$match = $router->match();
// not sure if code after this comment is the best way to handle matched routes
list( $controller, $action ) = explode( '#', $match['target'] );
if ( is_callable(array($controller, $action)) ) {
$obj = new $controller();
var_dump($obj);
call_user_func_array(array($obj,$action), array($match['params']));
} else if ($match['target']==''){
echo 'Error: no route was matched';
} else {
echo 'Error: can not call '.$controller.'#'.$action;
}
// content_controller class file is autoloaded
class home_controller {
public function index() {
echo 'hi from home';
}
}
and it works good. The home_controller class is supposed to be the default controller.
Problem is, when I remove the class home_controller
class home_controller {
public function index() {
echo 'hi from home';
}
}
and save it as a seprate file home_controller.php in app/controller directroy it does not work.
I understand that the router is unable to locate the home_controller class hence will not show it's content (if i directly include the file home_controller.php it again works as normal).
My question is, how do you map the home_controller as default, which is in a different directory?
It looks like you're not using composer for installing the package. It's standard way in PHP.
Go to your root directory of your project, open Command Line and type:
composer require altorouter/altorouter
You'll find the package name altorouter/altorouter
in composer.json
on Github page of package - here.
index.php
Now you have installed router package. Next step is adding all composer loaded files to your application. Just replace include 'library/altorouter.php';
with following:
<?php
# index.php
require_once __DIR__ . '/vendor/autoload.php';
Last step is tell composer where to find your classes.
Open composer.json
and add following section:
{
"autolaod": {
"classmap": ["app"]
}
}
Read more about classmap
option in documentation.
To update /vendor/autoload.php
with this option, just call from command line:
composer dump-autoload
That should be it. If you come at any troubles, let me know which point.