Skip to content

Commit 4dc10d3

Browse files
authored
Merge pull request #8642 from kenjis/docs-improve-news-tutorial
docs: improve News Tutorial code
2 parents 446aa0b + 31b484f commit 4dc10d3

File tree

9 files changed

+17
-12
lines changed

9 files changed

+17
-12
lines changed

user_guide_src/source/tutorial/news_section.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ some additional tools to make working with data simpler. Add the
9898
following code to your model.
9999

100100
.. literalinclude:: news_section/002.php
101-
:lines: 11-18
101+
:lines: 11-23
102102

103103
With this code, you can perform two different queries. You can get all
104104
news records, or get a news item by its slug. You might have

user_guide_src/source/tutorial/news_section/002.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ class NewsModel extends Model
88
{
99
protected $table = 'news';
1010

11+
/**
12+
* @param false|string $slug
13+
*
14+
* @return array|null
15+
*/
1116
public function getNews($slug = false)
1217
{
1318
if ($slug === false) {

user_guide_src/source/tutorial/news_section/003.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ public function index()
1010
{
1111
$model = model(NewsModel::class);
1212

13-
$data['news'] = $model->getNews();
13+
$data['news_list'] = $model->getNews();
1414
}
1515

16-
public function show($slug = null)
16+
public function show(?string $slug = null)
1717
{
1818
$model = model(NewsModel::class);
1919

user_guide_src/source/tutorial/news_section/004.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public function index()
1111
$model = model(NewsModel::class);
1212

1313
$data = [
14-
'news' => $model->getNews(),
15-
'title' => 'News archive',
14+
'news_list' => $model->getNews(),
15+
'title' => 'News archive',
1616
];
1717

1818
return view('templates/header', $data)

user_guide_src/source/tutorial/news_section/005.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<h2><?= esc($title) ?></h2>
22

3-
<?php if (! empty($news) && is_array($news)): ?>
3+
<?php if ($news_list !== []): ?>
44

5-
<?php foreach ($news as $news_item): ?>
5+
<?php foreach ($news_list as $news_item): ?>
66

77
<h3><?= esc($news_item['title']) ?></h3>
88

user_guide_src/source/tutorial/news_section/006.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ class News extends BaseController
99
{
1010
// ...
1111

12-
public function show($slug = null)
12+
public function show(?string $slug = null)
1313
{
1414
$model = model(NewsModel::class);
1515

1616
$data['news'] = $model->getNews($slug);
1717

18-
if (empty($data['news'])) {
18+
if ($data['news'] === null) {
1919
throw new PageNotFoundException('Cannot find the news item: ' . $slug);
2020
}
2121

user_guide_src/source/tutorial/static_pages.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ If the requested page doesn't exist, a "404 Page not found" error is shown.
159159
The first line in this method checks whether the page actually exists.
160160
PHP's native ``is_file()`` function is used to check whether the file
161161
is where it's expected to be. The ``PageNotFoundException`` is a CodeIgniter
162-
exception that causes the default error page to show.
162+
exception that causes the 404 Page Not Found error page to show.
163163

164164
In the header template, the ``$title`` variable was used to customize the
165165
page title. The value of title is defined in this method, but instead of

user_guide_src/source/tutorial/static_pages/001.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function index()
99
return view('welcome_message');
1010
}
1111

12-
public function view($page = 'home')
12+
public function view(string $page = 'home')
1313
{
1414
// ...
1515
}

user_guide_src/source/tutorial/static_pages/002.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Pages extends BaseController
88
{
99
// ...
1010

11-
public function view($page = 'home')
11+
public function view(string $page = 'home')
1212
{
1313
if (! is_file(APPPATH . 'Views/pages/' . $page . '.php')) {
1414
// Whoops, we don't have a page for that!

0 commit comments

Comments
 (0)