Skip to content

Commit

Permalink
Merge pull request #8642 from kenjis/docs-improve-news-tutorial
Browse files Browse the repository at this point in the history
docs: improve News Tutorial code
  • Loading branch information
kenjis authored Mar 25, 2024
2 parents 446aa0b + 31b484f commit 4dc10d3
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion user_guide_src/source/tutorial/news_section.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ some additional tools to make working with data simpler. Add the
following code to your model.

.. literalinclude:: news_section/002.php
:lines: 11-18
:lines: 11-23

With this code, you can perform two different queries. You can get all
news records, or get a news item by its slug. You might have
Expand Down
5 changes: 5 additions & 0 deletions user_guide_src/source/tutorial/news_section/002.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class NewsModel extends Model
{
protected $table = 'news';

/**
* @param false|string $slug
*
* @return array|null
*/
public function getNews($slug = false)
{
if ($slug === false) {
Expand Down
4 changes: 2 additions & 2 deletions user_guide_src/source/tutorial/news_section/003.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public function index()
{
$model = model(NewsModel::class);

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

public function show($slug = null)
public function show(?string $slug = null)
{
$model = model(NewsModel::class);

Expand Down
4 changes: 2 additions & 2 deletions user_guide_src/source/tutorial/news_section/004.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public function index()
$model = model(NewsModel::class);

$data = [
'news' => $model->getNews(),
'title' => 'News archive',
'news_list' => $model->getNews(),
'title' => 'News archive',
];

return view('templates/header', $data)
Expand Down
4 changes: 2 additions & 2 deletions user_guide_src/source/tutorial/news_section/005.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<h2><?= esc($title) ?></h2>

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions user_guide_src/source/tutorial/news_section/006.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class News extends BaseController
{
// ...

public function show($slug = null)
public function show(?string $slug = null)
{
$model = model(NewsModel::class);

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

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

Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/tutorial/static_pages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ If the requested page doesn't exist, a "404 Page not found" error is shown.
The first line in this method checks whether the page actually exists.
PHP's native ``is_file()`` function is used to check whether the file
is where it's expected to be. The ``PageNotFoundException`` is a CodeIgniter
exception that causes the default error page to show.
exception that causes the 404 Page Not Found error page to show.

In the header template, the ``$title`` variable was used to customize the
page title. The value of title is defined in this method, but instead of
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/tutorial/static_pages/001.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public function index()
return view('welcome_message');
}

public function view($page = 'home')
public function view(string $page = 'home')
{
// ...
}
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/tutorial/static_pages/002.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Pages extends BaseController
{
// ...

public function view($page = 'home')
public function view(string $page = 'home')
{
if (! is_file(APPPATH . 'Views/pages/' . $page . '.php')) {
// Whoops, we don't have a page for that!
Expand Down

0 comments on commit 4dc10d3

Please sign in to comment.