Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class router {
- /*
- * @the registry
- */
- private $registry;
- /*
- * @the controller path
- */
- private $path;
- private $args = array();
- public $file;
- public $controller;
- public $action;
- function __construct($registry) {
- $this->registry = $registry;
- }
- function setPath($path) {
- /*** check if path i sa directory ***/
- if (is_dir($path) == false)
- {
- throw new Exception ('Invalid controller path: `' . $path . '`');
- }
- /*** set the path ***/
- $this->path = $path;
- }
- public function loader()
- {
- /*** check the route ***/
- $this->getController();
- /*** if the file is not there diaf ***/
- if (is_readable($this->file) == false)
- {
- // TODO ? //
- // Route and Display a 404 Error Page when system is ready //
- echo "404: Not Found";
- } else {
- /*** include the controller ***/
- include $this->file;
- /*** a new controller class instance ***/
- $class = $this->controller . 'Controller';
- $controller = new $class($this->registry);
- /*** check if the action is callable ***/
- if (is_callable(array($controller, $this->action)) == false)
- {
- $action = 'news';
- }
- else
- {
- $action = $this->action;
- }
- /*** run the action ***/
- $controller->$action();
- }
- }
- private function getController() {
- /*** get the route from the url ***/
- $route = (empty($_GET['task'])) ? '' : $_GET['task'];
- if (empty($route))
- {
- $route = 'news';
- }
- else
- {
- /*** get the parts of the route ***/
- $parts = explode('/', $route);
- $this->controller = $parts[0];
- if(isset( $parts[1]))
- {
- $this->action = $parts[1];
- }
- }
- if (empty($this->controller))
- {
- $this->controller = 'news';
- }
- /*** Get action ***/
- if (empty($this->action))
- {
- $this->action = 'index';
- }
- /*** set the file path ***/
- $this->file = $this->path .'/'. $this->controller . 'Controller.php';
- }
- }
- <?php
- class template {
- /*
- * @the registry
- * @access private
- */
- private $registry;
- /*
- * @Variables array
- * @access private
- */
- private $vars = array();
- /**
- *
- * @constructor
- *
- * @access public
- *
- * @return void
- *
- */
- function __construct($registry) {
- $this->registry = $registry;
- }
- /**
- *
- * @set undefined vars
- *
- * @param string $index
- *
- * @param mixed $value
- *
- * @return void
- *
- */
- public function __set($index, $value)
- {
- $this->vars[$index] = $value;
- }
- public function show($name) {
- $path = SITE_PATH . '/system/views/' . $name . '.php';
- if (file_exists($path) == false)
- {
- throw new Exception('Template not found in '. $path);
- return false;
- }
- // Load variables
- foreach ($this->vars as $key => $value)
- {
- $key = $value;
- }
- include($path);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement