Skip to content

Commit ec81d88

Browse files
authored
Merge pull request #6934 from kenjis/docs-refactor-tutorial
docs: refactor Tutorial code
2 parents c6f3a67 + c5c0287 commit ec81d88

8 files changed

Lines changed: 33 additions & 16 deletions

File tree

user_guide_src/source/tutorial/create_news_items/004.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
// ...
44

5-
$routes->match(['get', 'post'], 'news/create', 'News::create');
6-
$routes->get('news/(:segment)', 'News::view/$1');
7-
$routes->get('news', 'News::index');
8-
$routes->get('pages', 'Pages::index');
9-
$routes->get('(:any)', 'Pages::view/$1');
5+
use App\Controllers\News;
6+
use App\Controllers\Pages;
7+
8+
$routes->match(['get', 'post'], 'news/create', [News::class, 'create']);
9+
$routes->get('news/(:segment)', [News::class, 'view']);
10+
$routes->get('news', [News::class, 'index']);
11+
$routes->get('pages', [Pages::class, 'index']);
12+
$routes->get('(:any)', [Pages::class, 'view']);
1013

1114
// ...

user_guide_src/source/tutorial/news_section.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ the views. Modify the ``index()`` method to look like this:
143143
The code above gets all news records from the model and assigns it to a
144144
variable. The value for the title is also assigned to the ``$data['title']``
145145
element and all data is passed to the views. You now need to create a
146-
view to render the news items. Create **app/Views/news/overview.php**
146+
view to render the news items. Create **app/Views/news/index.php**
147147
and add the next piece of code.
148148

149149
.. literalinclude:: news_section/005.php
@@ -166,6 +166,9 @@ add some code to the controller and create a new view. Go back to the
166166

167167
.. literalinclude:: news_section/006.php
168168

169+
Don't forget to add ``use CodeIgniter\Exceptions\PageNotFoundException;`` to import
170+
the ``PageNotFoundException`` class.
171+
169172
Instead of calling the ``getNews()`` method without a parameter, the
170173
``$slug`` variable is passed, so it will return the specific news item.
171174
The only thing left to do is create the corresponding view at

user_guide_src/source/tutorial/news_section/004.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function index()
1616
];
1717

1818
return view('templates/header', $data)
19-
. view('news/overview')
19+
. view('news/index')
2020
. view('templates/footer');
2121
}
2222

user_guide_src/source/tutorial/news_section/006.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Controllers;
44

55
use App\Models\NewsModel;
6+
use CodeIgniter\Exceptions\PageNotFoundException;
67

78
class News extends BaseController
89
{
@@ -15,7 +16,7 @@ public function view($slug = null)
1516
$data['news'] = $model->getNews($slug);
1617

1718
if (empty($data['news'])) {
18-
throw new \CodeIgniter\Exceptions\PageNotFoundException('Cannot find the news item: ' . $slug);
19+
throw new PageNotFoundException('Cannot find the news item: ' . $slug);
1920
}
2021

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

user_guide_src/source/tutorial/news_section/008.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
// ...
44

5-
$routes->get('news/(:segment)', 'News::view/$1');
6-
$routes->get('news', 'News::index');
7-
$routes->get('pages', 'Pages::index');
8-
$routes->get('(:any)', 'Pages::view/$1');
5+
use App\Controllers\News;
6+
use App\Controllers\Pages;
7+
8+
$routes->get('news/(:segment)', [News::class, 'view']);
9+
$routes->get('news', [News::class, 'index']);
10+
$routes->get('pages', [Pages::class, 'index']);
11+
$routes->get('(:any)', [Pages::class, 'view']);
912

1013
// ...

user_guide_src/source/tutorial/static_pages.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ in the ``Pages`` controller created above:
8989

9090
.. literalinclude:: static_pages/002.php
9191

92+
And add ``use CodeIgniter\Exceptions\PageNotFoundException;`` after the ``namespace`` line
93+
to import the ``PageNotFoundException`` class.
94+
9295
Now, when the requested page does exist, it is loaded, including the header and
9396
footer, and returned to the user. If a controller returns a string, it is
9497
displayed to the user.
@@ -144,7 +147,7 @@ Add the following lines, **after** the route directive for '/'.
144147

145148
CodeIgniter reads its routing rules from top to bottom and routes the
146149
request to the first matching rule. Each rule is a regular expression
147-
(left-side) mapped to a controller and method name separated by slashes
150+
(left-side) mapped to a controller and method name
148151
(right-side). When a request comes in, CodeIgniter looks for the first
149152
match, and calls the appropriate controller and method, possibly with
150153
arguments.

user_guide_src/source/tutorial/static_pages/002.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Controllers;
44

5+
use CodeIgniter\Exceptions\PageNotFoundException; // Add this line
6+
57
class Pages extends BaseController
68
{
79
// ...
@@ -10,7 +12,7 @@ public function view($page = 'home')
1012
{
1113
if (! is_file(APPPATH . 'Views/pages/' . $page . '.php')) {
1214
// Whoops, we don't have a page for that!
13-
throw new \CodeIgniter\Exceptions\PageNotFoundException($page);
15+
throw new PageNotFoundException($page);
1416
}
1517

1618
$data['title'] = ucfirst($page); // Capitalize the first letter
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
22

3-
$routes->get('pages', 'Pages::index');
4-
$routes->get('(:any)', 'Pages::view/$1');
3+
use App\Controllers\Pages;
4+
5+
$routes->get('pages', [Pages::class, 'index']);
6+
$routes->get('(:any)', [Pages::class, 'view']);

0 commit comments

Comments
 (0)