@@ -11,7 +11,7 @@ Create a database to work with
1111
1212The CodeIgniter installation assumes that you have set up an appropriate
1313database, as outlined in the :doc: `requirements </intro/requirements >`.
14- In this tutorial, we provide SQL code for a MySQL database, and
14+ In this tutorial, we provide SQL code for a MySQL database, and
1515we also assume that you have a suitable client for issuing database
1616commands (mysql, MySQL Workbench, or phpMyAdmin).
1717
@@ -35,13 +35,14 @@ and :doc:`Seeds <../dbmgmt/seeds>` to create more useful database setups later.
3535 KEY slug (slug)
3636 );
3737
38- A note of interest: a "slug", in the context of web publishing, is a
38+ A 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.
4040
41- The seed records might be something like:::
41+ The seed records might be something like:
4242
43+ ::
4344
44- INSERT INTO news VALUES
45+ INSERT INTO news VALUES
4546 (1,'Elvis sighted','elvis-sighted','Elvis was sighted at the Podunk internet cafe. It looked like he was writing a CodeIgniter app.'),
4647 (2,'Say it isn\'t so!','say-it-isnt-so','Scientists conclude that some programmers have a sense of humor.'),
4748 (3,'Caffeination, Yes!','caffeination-yes','World\'s largest coffee shop open onsite nested coffee shop for staff only.');
@@ -50,8 +51,11 @@ Connect to your database
5051-------------------------------------------------------
5152
5253The local configuration file, ``.env ``, that you created when you installed
53- CodeIgniter, should have the database property settings uncommented and
54- set appropriately for the database you want to use.::
54+ CodeIgniter, should have the database property settings uncommented and
55+ set appropriately for the database you want to use. Make sure you've configured
56+ your database properly as described :doc: `here <../database/configuration >`.
57+
58+ ::
5559
5660 database.default.hostname = localhost
5761 database.default.database = ci4tutorial
@@ -66,16 +70,16 @@ Instead of writing database operations right in the controller, queries
6670should be placed in a model, so they can easily be reused later. Models
6771are the place where you retrieve, insert, and update information in your
6872database or other data stores. They provide access to your data.
73+ You can read more about it :doc: `here </models/model >`.
6974
7075Open up the **app/Models/ ** directory and create a new file called
71- **NewsModel.php ** and add the following code. Make sure you've configured
72- your database properly as described :doc: `here <../database/configuration >`.
76+ **NewsModel.php ** and add the following code.
7377
7478::
7579
76- <?php namespace App\Models;
80+ <?php namespace App\Models;
7781
78- use CodeIgniter\Model;
82+ use CodeIgniter\Model;
7983
8084 class NewsModel extends Model
8185 {
@@ -107,8 +111,8 @@ following code to your model.
107111 }
108112
109113 return $this->asArray()
110- ->where(['slug' => $slug])
111- ->first();
114+ ->where(['slug' => $slug])
115+ ->first();
112116 }
113117
114118With this code, you can perform two different queries. You can get all
@@ -130,13 +134,14 @@ Now that the queries are written, the model should be tied to the views
130134that are going to display the news items to the user. This could be done
131135in our ``Pages `` controller created earlier, but for the sake of clarity,
132136a new ``News `` controller is defined. Create the new controller at
133- *app/Controllers/News.php *.
137+ ** app/Controllers/News.php * *.
134138
135139::
136140
137141 <?php namespace App\Controllers;
142+
138143 use App\Models\NewsModel;
139- use CodeIgniter\Controller;
144+ use CodeIgniter\Controller;
140145
141146 class News extends Controller
142147 {
@@ -181,7 +186,7 @@ the views. Modify the ``index()`` method to look like this::
181186
182187 echo view('templates/header', $data);
183188 echo view('news/overview', $data);
184- echo view('templates/footer');
189+ echo view('templates/footer', $data );
185190 }
186191
187192The code above gets all news records from the model and assigns it to a
@@ -192,18 +197,18 @@ and add the next piece of code.
192197
193198::
194199
195- <h2><?= $title ?></h2>
200+ <h2><?= esc( $title); ?></h2>
196201
197202 <?php if (! empty($news) && is_array($news)) : ?>
198203
199204 <?php foreach ($news as $news_item): ?>
200205
201- <h3><?= $news_item['title'] ?></h3>
206+ <h3><?= esc( $news_item['title']); ?></h3>
202207
203208 <div class="main">
204- <?= $news_item['body'] ?>
209+ <?= esc( $news_item['body']); ?>
205210 </div>
206- <p><a href="<?= '/news/'. $news_item['slug'] ?>">View article</a></p>
211+ <p><a href="/news/ <?= esc( $news_item['slug'], 'url'); ?>">View article</a></p>
207212
208213 <?php endforeach; ?>
209214
@@ -243,7 +248,7 @@ add some code to the controller and create a new view. Go back to the
243248
244249 echo view('templates/header', $data);
245250 echo view('news/view', $data);
246- echo view('templates/footer');
251+ echo view('templates/footer', $data );
247252 }
248253
249254Instead of calling the ``getNews() `` method without a parameter, the
@@ -253,9 +258,13 @@ The only thing left to do is create the corresponding view at
253258
254259::
255260
256- <?php
257- echo '<h2>'.$news['title'].'</h2>';
258- echo $news['body'];
261+ <h2><?= esc($news['title']); ?></h2>
262+ <?= esc($news['body']); ?>
263+
264+ .. 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 >`.
259268
260269Routing
261270-------------------------------------------------------
@@ -271,7 +280,7 @@ with a slug to the ``view()`` method in the ``News`` controller.
271280
272281 $routes->get('news/(:segment)', 'News::view/$1');
273282 $routes->get('news', 'News::index');
274- $routes->get('(:any)', 'Pages::showme /$1');
283+ $routes->get('(:any)', 'Pages::view /$1');
275284
276285Point your browser to your "news" page, i.e. ``localhost:8080/news ``,
277286you should see a list of the news items, each of which has a link
0 commit comments