Created
February 9, 2018 14:47
-
-
Save polodev/324900681fb4047bc2172cefb9434769 to your computer and use it in GitHub Desktop.
how to use altrouter with controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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