/** * Ensure the controller is called from the command line. */ public function __construct() { if (php_sapi_name() !== 'cli' || !empty($_SERVER['REMOTE_ADDR'])) { $err = new NotFoundError("Must be run from the command line."); return $err->serve(); } }
public function render() { if (!file_exists($this->file)) { $e = new NotFoundError("View unavailable."); $e->serve(); } // make any object vars available as globals to the view extract(get_object_vars($this)); require $this->file; }
/** * Instantiates the appropriate controller. */ public function delegate() { // route => /controller/method/args/args/ $fragments = explode('/', $this->route); $numFrags = count($fragments); // 'index' => 'IndexController' $controller = ucfirst($fragments[0]) . 'Controller'; // the default action is 'index()' $action = $numFrags > 1 ? $fragments[1] : 'index'; // if args are specified, then they'll be from n=2 to n-1 $args = $numFrags > 2 ? array_slice($fragments, 2, $numFrags - 1) : ''; // send a 404 error if the controller and/or method is invalid if (!class_exists($controller) || !is_callable(array($controller, $action))) { $e = new NotFoundError("<code>{$controller}->{$action}()</code> not found."); $e->serve(); } // instantiate class and call method $class = new $controller(); $class->{$action}($args); }
public function view($params = NULL) { // valid categories to search $categories = array("common_name_id" => "Common Name", "breed" => "Breed", "sex" => "Sex", "shelter_id" => "Shelter"); $v = new View("animal_index"); $v->title = "Browse"; $v->showBrowser = true; $animalModel = new AnimalModel(); // common name browser $commonModel = new CommonNameModel(); $v->commonNames = $commonModel->getAll(); // breed browser $breedModel = new BreedModel(); $v->breeds = $breedModel->getAll(); // shelter browser $shelterModel = new ShelterModel(); $v->shelters = $shelterModel->getAll(); $v->animals = array(); // default page if (!$params) { $v->animals = $animalModel->getLatestAnimals(Site::getInstance()->getNumAnimals()); return $v->render(); } // check params if (!isset($categories[$params[0]])) { $e = new NotFoundError("Invalid criteria"); return $e->serve(); } // sanitize the params for ($i = 0; $i < count($params); $i++) { $params[$i] = filter_var($params[$i], FILTER_SANITIZE_STRING); } $v->title = "Browsing by {$categories[$params[0]]}"; $v->animals = $animalModel->getByCategory($params[0], $params[1]); $v->render(); }