Skip to content

Commit b08fd34

Browse files
committed
Re-wrote parts of "Build Your First Application" [ci skip]
1 parent 1dc6216 commit b08fd34

6 files changed

Lines changed: 131 additions & 89 deletions

File tree

user_guide_src/source/outgoing/views.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ into the `$option` array in the third parameter.
180180

181181
echo view('blogview', $data, ['saveData' => true]);
182182

183-
Additionally, if you would like the default functionality of the view method to be that it does save the data
183+
Additionally, if you would like the default functionality of the view function to be that it does save the data
184184
between calls, you can set ``$saveData`` to **true** in **app/Config/Views.php**.
185185

186186
Creating Loops

user_guide_src/source/tutorial/conclusion.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ design patterns, which you can expand upon.
1010
Now that you've completed this tutorial, we recommend you check out the
1111
rest of the documentation. CodeIgniter is often praised because of its
1212
comprehensive documentation. Use this to your advantage and read the
13-
"Introduction" and "General Topics" sections thoroughly. You should read
13+
"Overview" and "General Topics" sections thoroughly. You should read
1414
the class and helper references when needed.
1515

1616
Every intermediate PHP programmer should be able to get the hang of

user_guide_src/source/tutorial/create_news_items.rst

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ the slug from our title in the model. Create a new view at
3333

3434
</form>
3535

36-
There is only one thing here that probably look unfamiliar to you: the
36+
There is only one thing here that probably look unfamiliar to you: the
3737
``\Config\Services::validation()->listErrors()`` function. It is used to report
3838
errors related to form validation.
3939

@@ -46,7 +46,6 @@ validation <../libraries/validation>` library to do this.
4646

4747
public function create()
4848
{
49-
helper('form');
5049
$model = new NewsModel();
5150

5251
if (! $this->validate([
@@ -57,7 +56,6 @@ validation <../libraries/validation>` library to do this.
5756
echo view('templates/header', ['title' => 'Create a news item']);
5857
echo view('news/create');
5958
echo view('templates/footer');
60-
6159
}
6260
else
6361
{
@@ -66,14 +64,14 @@ validation <../libraries/validation>` library to do this.
6664
'slug' => url_title($this->request->getVar('title')),
6765
'body' => $this->request->getVar('body'),
6866
]);
67+
6968
echo view('news/success');
7069
}
7170
}
7271

73-
The code above adds a lot of functionality. The first few lines load the
74-
form helper and the NewsModel. After that, the Controller-provided helper
75-
function is used to validate the $_POST fields. In this case, the title and
76-
text fields are required.
72+
The code above adds a lot of functionality. First we load the NewsModel.
73+
After that, the Controller-provided helper function is used to validate
74+
the $_POST fields. In this case, the title and text fields are required.
7775

7876
CodeIgniter has a powerful validation library as demonstrated
7977
above. You can read :doc:`more about this library
@@ -83,18 +81,20 @@ Continuing down, you can see a condition that checks whether the form
8381
validation ran successfully. If it did not, the form is displayed; if it
8482
was submitted **and** passed all the rules, the model is called. This
8583
takes care of passing the news item into the model.
86-
This contains a new function, url\_title(). This function -
84+
This contains a new function ``url_title()``. This function -
8785
provided by the :doc:`URL helper <../helpers/url_helper>` - strips down
8886
the string you pass it, replacing all spaces by dashes (-) and makes
8987
sure everything is in lowercase characters. This leaves you with a nice
9088
slug, perfect for creating URIs.
9189

9290
After this, a view is loaded to display a success message. Create a view at
93-
**app/Views/news/success.php** and write a success message.
91+
**app/Views/news/success.php** and write a success message.
9492

95-
This could be as simple as:::
93+
This could be as simple as:
94+
95+
::
9696

97-
News item created successfully.
97+
News item created successfully.
9898

9999
Model Updating
100100
-------------------------------------------------------
@@ -134,8 +134,9 @@ Routing
134134

135135
Before you can start adding news items into your CodeIgniter application
136136
you have to add an extra rule to **app/Config/Routes.php** file. Make sure your
137-
file contains the following. This makes sure CodeIgniter sees 'create'
138-
as a method instead of a news item's slug.
137+
file contains the following. This makes sure CodeIgniter sees ``create``
138+
as a method instead of a news item's slug. You can read more about different
139+
routing types :doc:`here </incoming/routing>`.
139140

140141
::
141142

@@ -158,15 +159,14 @@ Add some news and check out the different pages you made.
158159
:height: 415px
159160
:width: 45%
160161

161-
.. image:: ../images/tutorial9.png
162-
:align: left
163-
164-
165162
Congratulations
166163
-------------------------------------------------------
167164

168165
You just completed your first CodeIgniter4 application!
169166

170-
The image to the left shows your project's **app** folder,
167+
The image underneath shows your project's **app** folder,
171168
with all of the files that you created in green.
172169
The two modified configuration files (Database & Routes) are not shown.
170+
171+
.. image:: ../images/tutorial9.png
172+
:align: left

user_guide_src/source/tutorial/index.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ From your command line type the following:
5959

6060
::
6161

62-
composer create-project codeigniter4/appstarter ci-blog -s rc
62+
composer create-project codeigniter4/appstarter ci-blog -s rc
6363

6464
This creates a new folder, ci-blog, which contains your application code, with
6565
CodeIgniter installed in the vendor folder.
@@ -121,9 +121,9 @@ greeted by a screen looking something like this:
121121
There are a couple of things to note here:
122122

123123
1. Hovering over the red header at the top reveals a ``search`` link that will open up
124-
Google.com in a new tab and searching for the exception.
124+
Google.com in a new tab and searching for the exception.
125125
2. Clicking the ``arguments`` link on any line in the Backtrace will expand a list of
126-
the arguments that were passed into that function call.
126+
the arguments that were passed into that function call.
127127

128128
Everything else should be clear when you see it.
129129

user_guide_src/source/tutorial/news_section.rst

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Create a database to work with
1111

1212
The CodeIgniter installation assumes that you have set up an appropriate
1313
database, 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
1515
we also assume that you have a suitable client for issuing database
1616
commands (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
3939
user- 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

5253
The 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
6670
should be placed in a model, so they can easily be reused later. Models
6771
are the place where you retrieve, insert, and update information in your
6872
database or other data stores. They provide access to your data.
73+
You can read more about it :doc:`here </models/model>`.
6974

7075
Open 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

114118
With 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
130134
that are going to display the news items to the user. This could be done
131135
in our ``Pages`` controller created earlier, but for the sake of clarity,
132136
a 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

187192
The 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

249254
Instead 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

260269
Routing
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

276285
Point your browser to your "news" page, i.e. ``localhost:8080/news``,
277286
you should see a list of the news items, each of which has a link

0 commit comments

Comments
 (0)