@@ -26,14 +26,14 @@ and :doc:`Seeds <../dbmgmt/seeds>` to create more useful database setups later.
2626
2727::
2828
29- CREATE TABLE news (
30- id int(11) NOT NULL AUTO_INCREMENT,
31- title varchar(128) NOT NULL,
32- slug varchar(128) NOT NULL,
33- body text NOT NULL,
34- PRIMARY KEY (id),
35- KEY slug (slug)
36- );
29+ CREATE TABLE news (
30+ id int(11) NOT NULL AUTO_INCREMENT,
31+ title varchar(128) NOT NULL,
32+ slug varchar(128) NOT NULL,
33+ body text NOT NULL,
34+ PRIMARY KEY (id),
35+ KEY slug (slug)
36+ );
3737
3838A note of interest: a "slug", in the context of web publishing, is a
3939user- and SEO-friendly short text used in a URL to identify and describe a resource.
@@ -77,14 +77,14 @@ Open up the **app/Models/** directory and create a new file called
7777
7878::
7979
80- <?php namespace App\Models;
80+ <?php namespace App\Models;
8181
82- use CodeIgniter\Model;
82+ use CodeIgniter\Model;
8383
84- class NewsModel extends Model
85- {
86- protected $table = 'news';
87- }
84+ class NewsModel extends Model
85+ {
86+ protected $table = 'news';
87+ }
8888
8989This code looks similar to the controller code that was used earlier. It
9090creates a new model by extending ``CodeIgniter\Model `` and loads the database
@@ -103,17 +103,17 @@ following code to your model.
103103
104104::
105105
106- public function getNews($slug = false)
107- {
108- if ($slug === false)
109- {
110- return $this->findAll();
111- }
106+ public function getNews($slug = false)
107+ {
108+ if ($slug === false)
109+ {
110+ return $this->findAll();
111+ }
112112
113- return $this->asArray()
114- ->where(['slug' => $slug])
115- ->first();
116- }
113+ return $this->asArray()
114+ ->where(['slug' => $slug])
115+ ->first();
116+ }
117117
118118With this code, you can perform two different queries. You can get all
119119news records, or get a news item by its `slug <# >`_. You might have
@@ -138,27 +138,27 @@ a new ``News`` controller is defined. Create the new controller at
138138
139139::
140140
141- <?php namespace App\Controllers;
141+ <?php namespace App\Controllers;
142142
143- use App\Models\NewsModel;
144- use CodeIgniter\Controller;
143+ use App\Models\NewsModel;
144+ use CodeIgniter\Controller;
145145
146- class News extends Controller
147- {
148- public function index()
149- {
150- $model = new NewsModel();
146+ class News extends Controller
147+ {
148+ public function index()
149+ {
150+ $model = new NewsModel();
151151
152- $data['news'] = $model->getNews();
153- }
152+ $data['news'] = $model->getNews();
153+ }
154154
155- public function view($slug = null)
156- {
157- $model = new NewsModel();
155+ public function view($slug = null)
156+ {
157+ $model = new NewsModel();
158158
159- $data['news'] = $model->getNews($slug);
160- }
161- }
159+ $data['news'] = $model->getNews($slug);
160+ }
161+ }
162162
163163Looking at the code, you may see some similarity with the files we
164164created earlier. First, it extends a core CodeIgniter class, ``Controller ``,
@@ -175,19 +175,19 @@ Now the data is retrieved by the controller through our model, but
175175nothing is displayed yet. The next thing to do is, passing this data to
176176the views. Modify the ``index() `` method to look like this::
177177
178- public function index()
179- {
180- $model = new NewsModel();
178+ public function index()
179+ {
180+ $model = new NewsModel();
181181
182- $data = [
183- 'news' => $model->getNews(),
184- 'title' => 'News archive',
185- ];
182+ $data = [
183+ 'news' => $model->getNews(),
184+ 'title' => 'News archive',
185+ ];
186186
187- echo view('templates/header', $data);
188- echo view('news/overview', $data);
189- echo view('templates/footer', $data);
190- }
187+ echo view('templates/header', $data);
188+ echo view('news/overview', $data);
189+ echo view('templates/footer', $data);
190+ }
191191
192192The code above gets all news records from the model and assigns it to a
193193variable. The value for the title is also assigned to the ``$data['title'] ``
@@ -197,28 +197,28 @@ and add the next piece of code.
197197
198198::
199199
200- <h2><?= esc($title); ?></h2>
200+ <h2><?= esc($title); ?></h2>
201201
202- <?php if (! empty($news) && is_array($news)) : ?>
202+ <?php if (! empty($news) && is_array($news)) : ?>
203203
204- <?php foreach ($news as $news_item): ?>
204+ <?php foreach ($news as $news_item): ?>
205205
206- <h3><?= esc($news_item['title']); ?></h3>
206+ <h3><?= esc($news_item['title']); ?></h3>
207207
208- <div class="main">
209- <?= esc($news_item['body']); ?>
210- </div>
211- <p><a href="/news/<?= esc($news_item['slug'], 'url'); ?>">View article</a></p>
208+ <div class="main">
209+ <?= esc($news_item['body']); ?>
210+ </div>
211+ <p><a href="/news/<?= esc($news_item['slug'], 'url'); ?>">View article</a></p>
212212
213- <?php endforeach; ?>
213+ <?php endforeach; ?>
214214
215- <?php else : ?>
215+ <?php else : ?>
216216
217- <h3>No News</h3>
217+ <h3>No News</h3>
218218
219- <p>Unable to find any news for you.</p>
219+ <p>Unable to find any news for you.</p>
220220
221- <?php endif ?>
221+ <?php endif ?>
222222
223223Here, each news item is looped and displayed to the user. You can see we
224224wrote our template in PHP mixed with HTML. If you prefer to use a template
@@ -233,23 +233,23 @@ add some code to the controller and create a new view. Go back to the
233233
234234::
235235
236- public function view($slug = NULL)
237- {
238- $model = new NewsModel();
236+ public function view($slug = NULL)
237+ {
238+ $model = new NewsModel();
239239
240- $data['news'] = $model->getNews($slug);
240+ $data['news'] = $model->getNews($slug);
241241
242- if (empty($data['news']))
243- {
244- throw new \CodeIgniter\Exceptions\PageNotFoundException('Cannot find the news item: '. $slug);
245- }
242+ if (empty($data['news']))
243+ {
244+ throw new \CodeIgniter\Exceptions\PageNotFoundException('Cannot find the news item: '. $slug);
245+ }
246246
247- $data['title'] = $data['news']['title'];
247+ $data['title'] = $data['news']['title'];
248248
249- echo view('templates/header', $data);
250- echo view('news/view', $data);
251- echo view('templates/footer', $data);
252- }
249+ echo view('templates/header', $data);
250+ echo view('news/view', $data);
251+ echo view('templates/footer', $data);
252+ }
253253
254254Instead of calling the ``getNews() `` method without a parameter, the
255255``$slug `` variable is passed, so it will return the specific news item.
@@ -258,13 +258,13 @@ The only thing left to do is create the corresponding view at
258258
259259::
260260
261- <h2><?= esc($news['title']); ?></h2>
262- <?= esc($news['body']); ?>
261+ <h2><?= esc($news['title']); ?></h2>
262+ <?= esc($news['body']); ?>
263263
264264.. note :: We are again using using **esc()** to help prevent XSS attacks.
265- But this time we also passed "url" as a second parameter. That's because
266- attack patterns are different depending on the context in which the output
267- is used. You can read more about it :doc: `here </general/common_functions >`.
265+ But this time we also passed "url" as a second parameter. That's because
266+ attack patterns are different depending on the context in which the output
267+ is used. You can read more about it :doc: `here </general/common_functions >`.
268268
269269Routing
270270-------------------------------------------------------
@@ -278,9 +278,9 @@ with a slug to the ``view()`` method in the ``News`` controller.
278278
279279::
280280
281- $routes->get('news/(:segment)', 'News::view/$1');
282- $routes->get('news', 'News::index');
283- $routes->get('(:any)', 'Pages::view/$1');
281+ $routes->get('news/(:segment)', 'News::view/$1');
282+ $routes->get('news', 'News::index');
283+ $routes->get('(:any)', 'Pages::view/$1');
284284
285285Point your browser to your "news" page, i.e. ``localhost:8080/news ``,
286286you should see a list of the news items, each of which has a link
0 commit comments