Commits

Anonymous committed bbdfd7b

Página com a lista de posts.

Removi arquivos desnecessários.
Mudei a rota padrão.
Criei o controller e a model para os posts.
Criar uma view básica para os posts.
Criei o helper datas_helper.

Comments (0)

Files changed (9)

application/config/routes.php

 |
 */
 
-$route['default_controller'] = "welcome";
+$route['default_controller'] = "posts";
 $route['404_override'] = '';
 
 /* End of file routes.php */

application/controllers/calculadora.php

-<?php
-
-class Calculadora extends CI_Controller
-{    
-    public function adicao($n1, $n2)
-    {
-        $data = array(
-            'n1' => $n1,
-            'n2' => $n2,
-            'resultado' => $n1 + $n2,
-            'operacao' => 'soma'
-        );
-
-        $this->load->view('calculadora', $data);
-    }
-
-    public function subtracao($n1, $n2)
-    {
-        $data = array(
-            'n1' => $n1,
-            'n2' => $n2,
-            'resultado' => $n1 - $n2,
-            'operacao' => 'subtracao'
-        );
-
-        $this->load->view('calculadora', $data);
-    }
-
-}

application/controllers/posts.php

+<?php
+
+class Posts extends CI_Controller
+{
+
+    public function __construct()
+    {
+        parent::__construct();
+
+        $this->output->enable_profiler(FALSE);
+    }
+
+    /**
+     * index
+     * 
+     * Na index são listados todos os posts
+     * 
+     * @access public
+     * @return void
+     */
+    public function index()
+    {
+        $this->load->model('posts_model');
+        $this->load->helper('datas');
+
+        $data = array();
+
+        $data['posts'] = $this->posts_model->pegar_todos();
+
+        $this->load->view('posts/index', $data);
+    }
+}

application/controllers/welcome.php

-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
-
-class Welcome extends CI_Controller {
-
-	/**
-	 * Index Page for this controller.
-	 *
-	 * Maps to the following URL
-	 * 		http://example.com/index.php/welcome
-	 *	- or -  
-	 * 		http://example.com/index.php/welcome/index
-	 *	- or -
-	 * Since this controller is set as the default controller in 
-	 * config/routes.php, it's displayed at http://example.com/
-	 *
-	 * So any other public methods not prefixed with an underscore will
-	 * map to /index.php/welcome/<method_name>
-	 * @see http://codeigniter.com/user_guide/general/urls.html
-	 */
-	public function index()
-	{
-		$this->load->view('welcome_message');
-	}
-
-    public function meumetodo($nome = "CodeIgniter", $sobrenome = "")
-    {
-        echo "Ola, {$nome} {$sobrenome}!";
-    }
-}
-
-/* End of file welcome.php */
-/* Location: ./application/controllers/welcome.php */

application/helpers/datas_helper.php

+<?php
+
+/**
+ * formatar_data_exibicao
+ *
+ * Recebe uma data no formado "YYYY-MM-DD HH:MM:SS"
+ * E formata para "DD/MM/YYYY às HH:MM"
+ * 
+ * @param string $data 
+ * @access public
+ * @return string
+ */
+function formatar_data_exibicao($data)
+{
+    $d = new DateTime($data);
+
+    return $d->format('d/m/Y \à\s H:i');
+}

application/models/posts_model.php

+<?php
+
+class Posts_model extends CI_Model
+{
+    public function pegar_todos()
+    {
+        $this->db->order_by('publicacao DESC');
+
+        return $this->db->get('posts')->result();
+    }
+}

application/views/calculadora.php

-<!DOCTYPE html>
-<html>
-    <head>
-        <meta charset="utf-8" />
-        <title>Calculadora</title>
-    </head>
-    <body>
-        <h1>Calculadora</h1>
-
-        <p>
-            A <?php echo $operacao; ?>
-            de <?php echo $n1; ?>
-            e <?php echo $n2; ?>
-            é <?php echo $resultado; ?>
-        </p>
-    </body>
-</html>

application/views/posts/index.php

+<!DOCTYPE html>
+<html>
+    <head>
+        <meta charset="utf-8">
+        <title>Blog CI</title>
+    </head>
+    <body>
+        <h1>Blog CI</h1>
+
+        <?php foreach ($posts as $p) : ?>
+            <h2><?php echo $p->titulo; ?></h2>
+            <p>
+                <em>
+                    Publicado em:
+                    <?php echo formatar_data_exibicao($p->publicacao); ?>
+                </em>
+            </p>
+            <p><strong>Autor: <?php echo $p->autor; ?></strong></p>
+            <div>
+                <?php echo $p->texto; ?>
+            </div>
+            <p>Tags: <?php echo $p->tags; ?></p>
+        <?php endforeach; ?>
+    </body>
+</html>

application/views/welcome_message.php

-<!DOCTYPE html>
-<html lang="en">
-<head>
-	<meta charset="utf-8">
-	<title>Welcome to CodeIgniter</title>
-
-	<style type="text/css">
-
-	::selection{ background-color: #E13300; color: white; }
-	::moz-selection{ background-color: #E13300; color: white; }
-	::webkit-selection{ background-color: #E13300; color: white; }
-
-	body {
-		background-color: #fff;
-		margin: 40px;
-		font: 13px/20px normal Helvetica, Arial, sans-serif;
-		color: #4F5155;
-	}
-
-	a {
-		color: #003399;
-		background-color: transparent;
-		font-weight: normal;
-	}
-
-	h1 {
-		color: #444;
-		background-color: transparent;
-		border-bottom: 1px solid #D0D0D0;
-		font-size: 19px;
-		font-weight: normal;
-		margin: 0 0 14px 0;
-		padding: 14px 15px 10px 15px;
-	}
-
-	code {
-		font-family: Consolas, Monaco, Courier New, Courier, monospace;
-		font-size: 12px;
-		background-color: #f9f9f9;
-		border: 1px solid #D0D0D0;
-		color: #002166;
-		display: block;
-		margin: 14px 0 14px 0;
-		padding: 12px 10px 12px 10px;
-	}
-
-	#body{
-		margin: 0 15px 0 15px;
-	}
-	
-	p.footer{
-		text-align: right;
-		font-size: 11px;
-		border-top: 1px solid #D0D0D0;
-		line-height: 32px;
-		padding: 0 10px 0 10px;
-		margin: 20px 0 0 0;
-	}
-	
-	#container{
-		margin: 10px;
-		border: 1px solid #D0D0D0;
-		-webkit-box-shadow: 0 0 8px #D0D0D0;
-	}
-	</style>
-</head>
-<body>
-
-<div id="container">
-	<h1>Bem vindo ao Turbine-se com CodeIgniter!</h1>
-
-	<div id="body">
-		<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
-
-		<p>If you would like to edit this page you'll find it located at:</p>
-		<code>application/views/welcome_message.php</code>
-
-		<p>The corresponding controller for this page is found at:</p>
-		<code>application/controllers/welcome.php</code>
-
-		<p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p>
-	</div>
-
-	<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
-</div>
-
-</body>
-</html>