Skip to content

Instantly share code, notes, and snippets.

@polodev
Created February 9, 2018 14:47
Show Gist options
  • Save polodev/324900681fb4047bc2172cefb9434769 to your computer and use it in GitHub Desktop.
Save polodev/324900681fb4047bc2172cefb9434769 to your computer and use it in GitHub Desktop.
how to use altrouter with controller
<?php
require 'AltoRouter.php';
$router = new AltoRouter();
$router->map('GET','/', 'home_controller#index', 'home');
$router->map('GET','/dashboard/hello/world/from/dhaka', 'home_controller#hello', 'hello');
$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();
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;
}
class home_controller {
public function index() {
include "home.php";
exit();
}
public function hello() {
echo 'you are in dhaka';
}
}
class content_controller {
public function display_item($content) {
print_r($content); // associative array with
echo 'hello from display_item';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment