├── .htaccess ├── App ├── .htaccess ├── Config │ └── config.ini ├── Controllers │ ├── Home.php │ ├── Profile.php │ └── index.html ├── Interfaces │ ├── Crud.php │ └── index.html ├── Models │ ├── Admin │ │ └── User.php │ ├── Home.php │ ├── User.php │ └── index.html └── Views │ ├── admin.php │ ├── errors │ └── 404.php │ ├── home.php │ ├── home │ └── index.php │ └── index.html ├── Core ├── .htaccess ├── App.php ├── Controller.php ├── Database.php ├── Model.php └── View.php ├── README.md ├── composer.json └── public ├── .htaccess └── index.php /.htaccess: -------------------------------------------------------------------------------- 1 | Options -Indexes 2 | -------------------------------------------------------------------------------- /App/.htaccess: -------------------------------------------------------------------------------- 1 | Options -Indexes 2 | -------------------------------------------------------------------------------- /App/Config/config.ini: -------------------------------------------------------------------------------- 1 | [database] 2 | adapter = Mysql 3 | host = localhost 4 | user = root 5 | password = admin 6 | database = mvc 7 | -------------------------------------------------------------------------------- /App/Controllers/Home.php: -------------------------------------------------------------------------------- 1 | prepare($sql); 16 | $query->execute(); 17 | return $query->fetchAll(); 18 | } 19 | catch(\PDOException $e) 20 | { 21 | print "Error!: " . $e->getMessage(); 22 | } 23 | } 24 | 25 | public static function getById($id) 26 | { 27 | try { 28 | $connection = Database::instance(); 29 | $sql = "SELECT * from usuarios WHERE id = ?"; 30 | $query = $connection->prepare($sql); 31 | $query->bindParam(1, $id, \PDO::PARAM_INT); 32 | $query->execute(); 33 | return $query->fetch(); 34 | } 35 | catch(\PDOException $e) 36 | { 37 | print "Error!: " . $e->getMessage(); 38 | } 39 | } 40 | 41 | public static function insert($user) 42 | { 43 | 44 | } 45 | 46 | public static function update($user) 47 | { 48 | 49 | } 50 | 51 | public static function delete($id) 52 | { 53 | 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /App/Models/index.html: -------------------------------------------------------------------------------- 1 | Access denied 2 | -------------------------------------------------------------------------------- /App/Views/admin.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | <?php echo $title ?> 6 | 7 | 8 | 9 | 10 | 11 | 12 | 15 | 18 | 19 | 20 | 21 | 25 | 26 | 27 | 28 | 29 | 32 | 33 |
13 | Id 14 | 16 | Nombre 17 |
34 | 35 | 36 | -------------------------------------------------------------------------------- /App/Views/errors/404.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | NOT FOUND 6 | 7 | 8 | 404 PAGE NOT FOUND 9 | 10 | 11 | -------------------------------------------------------------------------------- /App/Views/home.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | <?php echo $title ?> 6 | 7 | 8 | Welcome 9 | 10 | 11 | -------------------------------------------------------------------------------- /App/Views/home/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | MVC PHP Unodepiera 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /App/Views/index.html: -------------------------------------------------------------------------------- 1 | Access denied 2 | -------------------------------------------------------------------------------- /Core/.htaccess: -------------------------------------------------------------------------------- 1 | Options -Indexes 2 | -------------------------------------------------------------------------------- /Core/App.php: -------------------------------------------------------------------------------- 1 | parseUrl(); 45 | 46 | //comprobamos que exista el archivo en el directorio Controllers 47 | if(file_exists(self::CONTROLLERS_PATH.ucfirst($url[0]) . ".php")) 48 | { 49 | //nombre del archivo a llamar 50 | $this->_controller = ucfirst($url[0]); 51 | //eliminamos el controlador de url, así sólo nos quedaran los parámetros del método 52 | unset($url[0]); 53 | } 54 | else 55 | { 56 | include APPPATH . "/Views/errors/404.php"; 57 | exit; 58 | } 59 | 60 | //obtenemos la clase con su espacio de nombres 61 | $fullClass = self::NAMESPACE_CONTROLLERS.$this->_controller; 62 | 63 | //asociamos la instancia a $this->_controller 64 | $this->_controller = new $fullClass; 65 | 66 | //si existe el segundo segmento comprobamos que el método exista en esa clase 67 | if(isset($url[1])) 68 | { 69 | 70 | //aquí tenemos el método 71 | $this->_method = $url[1]; 72 | if(method_exists($this->_controller, $url[1])) 73 | { 74 | //eliminamos el método de url, así sólo nos quedaran los parámetros del método 75 | unset($url[1]); 76 | } 77 | else 78 | { 79 | throw new \Exception("Error Processing Method {$this->_method}", 1); 80 | } 81 | } 82 | //asociamos el resto de segmentos a $this->_params para pasarlos al método llamado, por defecto será un array vacío 83 | $this->_params = $url ? array_values($url) : []; 84 | } 85 | 86 | /** 87 | * [parseUrl Parseamos la url en trozos] 88 | * @return [type] [description] 89 | */ 90 | public function parseUrl() 91 | { 92 | if(isset($_GET["url"])) 93 | { 94 | return explode("/", filter_var(rtrim($_GET["url"], "/"), FILTER_SANITIZE_URL)); 95 | } 96 | } 97 | 98 | /** 99 | * [render lanzamos el controlador/método que se ha llamado con los parámetros] 100 | */ 101 | public function render() 102 | { 103 | call_user_func_array([$this->_controller, $this->_method], $this->_params); 104 | } 105 | 106 | /** 107 | * [getConfig Obtenemos la configuración de la app] 108 | * @return [Array] [Array con la Config] 109 | */ 110 | public static function getConfig() 111 | { 112 | return parse_ini_file(APPPATH . '/Config/Config.ini'); 113 | } 114 | 115 | /** 116 | * [getController Devolvemos el controlador actual] 117 | * @return [type] [String] 118 | */ 119 | public function getController() 120 | { 121 | return $this->_controller; 122 | } 123 | 124 | /** 125 | * [getMethod Devolvemos el método actual] 126 | * @return [type] [String] 127 | */ 128 | public function getMethod() 129 | { 130 | return $this->_method; 131 | } 132 | 133 | /** 134 | * [getParams description] 135 | * @return [type] [Array] 136 | */ 137 | public function getParams() 138 | { 139 | return $this->_params; 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /Core/Controller.php: -------------------------------------------------------------------------------- 1 | _dbHost = $config["host"]; 64 | $this->_dbUser = $config["user"]; 65 | $this->_dbPassword = $config["password"]; 66 | $this->_dbName = $config["database"]; 67 | 68 | $this->_connection = new \PDO('mysql:host='.$this->_dbHost.'; dbname='.$this->_dbName, $this->_dbUser, $this->_dbPassword); 69 | $this->_connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); 70 | $this->_connection->exec("SET CHARACTER SET utf8"); 71 | } 72 | catch (\PDOException $e) 73 | { 74 | print "Error!: " . $e->getMessage(); 75 | die(); 76 | } 77 | } 78 | 79 | /** 80 | * [prepare] 81 | * @param [type] $sql [description] 82 | * @return [type] [description] 83 | */ 84 | public function prepare($sql) 85 | { 86 | return $this->_connection->prepare($sql); 87 | } 88 | 89 | /** 90 | * [instance singleton] 91 | * @return [object] [class database] 92 | */ 93 | public static function instance() 94 | { 95 | if (!isset(self::$_instance)) 96 | { 97 | $class = __CLASS__; 98 | self::$_instance = new $class; 99 | } 100 | return self::$_instance; 101 | } 102 | 103 | /** 104 | * [__clone Evita que el objeto se pueda clonar] 105 | * @return [type] [message] 106 | */ 107 | public function __clone() 108 | { 109 | trigger_error('La clonación de este objeto no está permitida', E_USER_ERROR); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /Core/Model.php: -------------------------------------------------------------------------------- 1 | render(); --------------------------------------------------------------------------------