Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1994d95

Browse files
View class
1 parent bfe145d commit 1994d95

File tree

5 files changed

+100
-4
lines changed

5 files changed

+100
-4
lines changed

‎app/controllers/Home/HomeController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ class HomeController extends Controller
99
*/
1010
public function __construct()
1111
{
12-
var_dump(__CLASS__);
12+
$lenguajes_favoritos = array('Javascript', 'Php');
13+
$params = array('nombre' => 'Juan', 'lenguajes' => $lenguajes_favoritos);
14+
$this->render(__CLASS__, $params);
1315
}
1416

1517
/**
1618
* Método estándar
1719
*/
1820
public function exec()
1921
{
20-
echo'<h1>Hola mundo!</h1>';
22+
2123
}
2224
}

‎app/views/Home/Home.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Home</title>
5+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
6+
</head>
7+
<body>
8+
<div class="container">
9+
<div class="jumbotron">
10+
<h1>Hola <?php echo $nombre; ?></h1>
11+
</div>
12+
<div>
13+
<ul>
14+
<?php
15+
foreach($lenguajes as $lenguaje)
16+
{
17+
echo "<li>$lenguaje</li>";
18+
}
19+
?>
20+
</ul>
21+
</div>
22+
</div>
23+
</body>
24+
</html>

‎system/config.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
// Valores de rutas
1010
/////////////////////////////////////
1111

12+
define('ROOT', $_SERVER['DOCUMENT_ROOT']);
13+
14+
define('PATH_VIEWS', 'php-mvc/app/views/');
15+
1216
define('PATH_CONTROLLERS', 'app/controllers/');
1317

1418
//////////////////////////////////////

‎system/core/Controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ abstract class Controller
1212
/**
1313
* Inicializa la vista
1414
*/
15-
public function __construct()
15+
public function render($controller_name = '', $params = array())
1616
{
17-
17+
$this->view = newView($controller_name, $params);
1818
}
1919

2020
/**

‎system/core/View.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Vista base
4+
*/
5+
class View
6+
{
7+
/**
8+
* string
9+
*/
10+
protected $template;
11+
12+
/**
13+
* string
14+
*/
15+
protected $controller_name;
16+
17+
/**
18+
* array
19+
*/
20+
protected $params;
21+
22+
/**
23+
* Inicializa valores y el render
24+
* @param string $controller_name
25+
* @param array $params. Opcional
26+
*/
27+
public function __construct($controller_name, $params = array())
28+
{
29+
$this->controller_name = $controller_name;
30+
$this->params = $params;
31+
$this->render();
32+
}
33+
34+
/**
35+
* Muestra la vista según el controlador que hizo la petición
36+
*/
37+
protected function render()
38+
{
39+
if(class_exists($this->controller_name)){
40+
$file_name = str_replace('Controller', '', $this->controller_name);
41+
$this->template = $this->getContentTemplate($file_name);
42+
echo $this->template;
43+
}else{
44+
throw new Exception("Error No existe $controller_name");
45+
}
46+
}
47+
48+
/**
49+
* Retorna el render de una vista
50+
*/
51+
protected function getContentTemplate($file_name)
52+
{
53+
$file_path = ROOT . '/' . PATH_VIEWS . "$file_name/$file_name" . '.php';
54+
if(is_file($file_path)){
55+
extract($this->params);
56+
ob_start();
57+
require($file_path);
58+
$template = ob_get_contents();
59+
ob_end_clean();
60+
return $template;
61+
}else{
62+
throw new Exception("Error No existe $file_path");
63+
}
64+
}
65+
66+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /