anvil_php/app/core/Controller.php

113 lines
2.8 KiB
PHP
Raw Normal View History

2012-06-11 17:08:59 +00:00
<?php
class Controller {
2014-08-19 14:10:22 +00:00
protected $anvil;
public $default_function = '';
public function __construct($a) {
$this->anvil = $a;
}
public function index() {
$func_name = $this->default_function;
if(isset($this->anvil->request->uri_array[0])) {
if($this->anvil->request->uri_array[0] == $this->anvil->active_controller) {
if(isset($this->anvil->request->uri_array[1])) {
$func_name = $this->anvil->request->uri_array[1];
}
} else if(!empty($this->anvil->request->uri_array[0])) {
$func_name = $this->anvil->request->uri_array[0];
}
}
if(method_exists($this, $func_name)) {
return $this->$func_name();
} else {
header("HTTP/1.0 404 Not Found");
echo "Page Not Found";
exit;
}
}
2012-06-11 17:08:59 +00:00
2015-10-22 16:02:50 +00:00
public function load_helpers($helper=NULL) {
$this->load_helper($helper);
}
public function load_helper($helper=NULL) {
// All helpers end with '_helper'
if(is_array($helper)) {
foreach($helper as $k=>$m) {
$helper[$k]=$m."_helper";
}
} else {
$helper.="_helper";
}
$this->_load_files($helper, "helpers");
}
public function load_models($model=NULL) {
$this->load_model($model);
}
2012-06-11 17:08:59 +00:00
public function load_model($model=NULL) {
// All models end with '_model'
if(is_array($model)) {
foreach($model as $k=>$m) {
$model[$k]=$m."_model";
}
} else {
$model.="_model";
}
$this->_load_files($model, "models");
}
public function load_libraries($library=NULL) {
$this->load_library($library);
}
2012-06-11 17:08:59 +00:00
public function load_library($library=NULL) {
// All libraries end with '_library'
if(is_array($library)) {
foreach($library as $k=>$l) {
$library[$k]=$l."_library";
}
} else {
$library.="_library";
}
$this->_load_files($library, "libraries");
}
public function load_views($views=NULL, $vars=array()) {
$this->load_view($views,$vars);
}
public function load_view($views=NULL, $vars=array()) {
2012-06-11 17:08:59 +00:00
// No restrictions on view names
$this->_load_files($views, "views", true, $vars);
2012-06-11 17:08:59 +00:00
}
// Runs through a potential array of files
// Checks for existence, then _load_file
public function _load_files($a=null, $func=null, $multi=false, $vars=array()) {
2012-06-11 17:08:59 +00:00
if(isset($a) && isset($func)) {
if(is_array($a)) {
foreach($a as $aa) {
$f = APP_ROOT."/".$func."/".$aa.".php";
$this->_load_file($f, ($multi===true), $vars);
2012-06-11 17:08:59 +00:00
}
} else {
$f = APP_ROOT."/".$func."/".$a.".php";
$this->_load_file($f, ($multi===true), $vars);
2012-06-11 17:08:59 +00:00
}
}
}
// Checks if the file exists and includes it
public function _load_file($filename=NULL,$multi=false,$vars=null) {
if(isset($vars)&&is_array($vars)) {
extract($vars);
}
2012-06-11 17:08:59 +00:00
if(isset($filename) && file_exists($filename)) {
if($multi) {
include($filename);
} else {
require_once($filename);
}
}
}
}