Skip to content

Commit 620a560

Browse files
committed
docs: update coding style and its explanation
1 parent 96ffbf9 commit 620a560

3 files changed

Lines changed: 29 additions & 27 deletions

File tree

user_guide_src/source/tutorial/create_news_items.rst

Lines changed: 3 additions & 4 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
<h2><?= esc($title) ?></h2>
3535

36-
<?= \Config\Services::validation()->listErrors() ?>
36+
<?= service('validation')->listErrors() ?>
3737

3838
<form action="/news/create" method="post">
3939
<?= csrf_field() ?>
@@ -48,7 +48,7 @@ the slug from our title in the model. Create a new view at
4848
</form>
4949

5050
There are probably only two things here that look unfamiliar. The
51-
``\Config\Services::validation()->listErrors()`` function is used to report
51+
``service('validation')->listErrors()`` function is used to report
5252
errors related to form validation. The ``csrf_field()`` function creates
5353
a hidden input with a CSRF token that helps protect against some common attacks.
5454

@@ -61,7 +61,7 @@ validation <../libraries/validation>` library to do this.
6161

6262
public function create()
6363
{
64-
$model = new NewsModel();
64+
$model = model(NewsModel::class);
6565

6666
if ($this->request->getMethod() === 'post' && $this->validate([
6767
'title' => 'required|min_length[3]|max_length[255]',
@@ -74,7 +74,6 @@ validation <../libraries/validation>` library to do this.
7474
]);
7575

7676
echo view('news/success');
77-
7877
} else {
7978
echo view('templates/header', ['title' => 'Create a news item']);
8079
echo view('news/create');

user_guide_src/source/tutorial/news_section.rst

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ and :doc:`Seeds <../dbmgmt/seeds>` to create more useful database setups later.
2727
::
2828

2929
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,
30+
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
31+
title VARCHAR(128) NOT NULL,
32+
slug VARCHAR(128) NOT NULL,
33+
body TEXT NOT NULL,
3434
PRIMARY KEY (id),
3535
KEY slug (slug)
3636
);
@@ -111,22 +111,20 @@ following code to your model.
111111
return $this->findAll();
112112
}
113113

114-
return $this->asArray()
115-
->where(['slug' => $slug])
116-
->first();
114+
return return $this->where(['slug' => $slug])->first();
117115
}
118116

119117
With this code, you can perform two different queries. You can get all
120-
news records, or get a news item by its `slug <#>`_. You might have
121-
noticed that the ``$slug`` variable wasn't sanitized before running the
118+
news records, or get a news item by its slug. You might have
119+
noticed that the ``$slug`` variable wasn't escaped before running the
122120
query; :doc:`Query Builder <../database/query_builder>` does this for you.
123121

124122
The two methods used here, ``findAll()`` and ``first()``, are provided
125-
by the Model class. They already know the table to use based on the ``$table``
123+
by the ``CodeIgniter\Model`` class. They already know the table to use based on the ``$table``
126124
property we set in **NewsModel** class, earlier. They are helper methods
127125
that use the Query Builder to run their commands on the current table, and
128126
returning an array of results in the format of your choice. In this example,
129-
``findAll()`` returns an array of objects.
127+
``findAll()`` returns an array of array.
130128

131129
Display the news
132130
-------------------------------------------------------
@@ -150,14 +148,14 @@ a new ``News`` controller is defined. Create the new controller at
150148
{
151149
public function index()
152150
{
153-
$model = new NewsModel();
151+
$model = model(NewsModel::class);
154152

155153
$data['news'] = $model->getNews();
156154
}
157155

158156
public function view($slug = null)
159157
{
160-
$model = new NewsModel();
158+
$model = model(NewsModel::class);
161159

162160
$data['news'] = $model->getNews($slug);
163161
}
@@ -170,7 +168,13 @@ access to the current ``Request`` and ``Response`` objects, as well as the
170168
``Logger`` class, for saving information to disk.
171169

172170
Next, there are two methods, one to view all news items, and one for a specific
173-
news item. You can see that the ``$slug`` variable is passed to the model's
171+
news item.
172+
173+
Next, the ``model()`` function is used to create the **NewsModel** instance.
174+
This is a helper function. You can read more about it :doc:`here </general/common_functions>`.
175+
You could also write ``$model = new NewsModel();``, if you don't use it.
176+
177+
You can see that the ``$slug`` variable is passed to the model's
174178
method in the second method. The model is using this slug to identify the
175179
news item to be returned.
176180

@@ -180,7 +184,7 @@ the views. Modify the ``index()`` method to look like this::
180184

181185
public function index()
182186
{
183-
$model = new NewsModel();
187+
$model = model(NewsModel::class);
184188

185189
$data = [
186190
'news' => $model->getNews(),
@@ -202,7 +206,7 @@ and add the next piece of code.
202206

203207
<h2><?= esc($title) ?></h2>
204208

205-
<?php if (! empty($news) && is_array($news)) : ?>
209+
<?php if (! empty($news) && is_array($news)): ?>
206210

207211
<?php foreach ($news as $news_item): ?>
208212

@@ -215,7 +219,7 @@ and add the next piece of code.
215219

216220
<?php endforeach; ?>
217221

218-
<?php else : ?>
222+
<?php else: ?>
219223

220224
<h3>No News</h3>
221225

@@ -244,13 +248,12 @@ add some code to the controller and create a new view. Go back to the
244248

245249
public function view($slug = null)
246250
{
247-
$model = new NewsModel();
251+
$model = model(NewsModel::class);
248252

249253
$data['news'] = $model->getNews($slug);
250254

251-
if (empty($data['news']))
252-
{
253-
throw new \CodeIgniter\Exceptions\PageNotFoundException('Cannot find the news item: '. $slug);
255+
if (empty($data['news'])) {
256+
throw new \CodeIgniter\Exceptions\PageNotFoundException('Cannot find the news item: ' . $slug);
254257
}
255258

256259
$data['title'] = $data['news']['title'];

user_guide_src/source/tutorial/static_pages.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,15 @@ in the ``Pages`` controller created above:
129129

130130
public function view($page = 'home')
131131
{
132-
if ( ! is_file(APPPATH.'/Views/pages/'.$page.'.php')) {
132+
if (! is_file(APPPATH . 'Views/pages/' . $page . '.php')) {
133133
// Whoops, we don't have a page for that!
134134
throw new \CodeIgniter\Exceptions\PageNotFoundException($page);
135135
}
136136

137137
$data['title'] = ucfirst($page); // Capitalize the first letter
138138

139139
echo view('templates/header', $data);
140-
echo view('pages/'.$page, $data);
140+
echo view('pages/' . $page, $data);
141141
echo view('templates/footer', $data);
142142
}
143143

0 commit comments

Comments
 (0)