Skip to content

Commit 911e46f

Browse files
authored
Merge pull request #1693 from jim-parry/docs/tutorial
Docs/tutorial
2 parents 9f132fc + 7256586 commit 911e46f

File tree

13 files changed

+191
-101
lines changed

13 files changed

+191
-101
lines changed
46.6 KB
Loading
11.2 KB
Loading
38.6 KB
Loading
31 KB
Loading
11.6 KB
Loading
33.1 KB
Loading

user_guide_src/source/installation/index.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ Installation
44

55
CodeIgniter4 can be installed in a number of different ways: manually,
66
using `Composer <https://getcomposer.org>`_, or using
7-
`Git <https://git-scm.com/>`_. If you are not sure which is most
8-
appropriate for you, read the introduction for
9-
each technique, and consider their pros and cons.
7+
`Git <https://git-scm.com/>`_.
8+
Which is right for you?
109

11-
Once installed, there are several ways to run a
12-
CodeIgniter4 app. Read on :)
10+
- If you would like the simple "download & go" install that CodeIgniter3
11+
is known for, choose the manual installation.
12+
- If you plan to add third party packages to your project, we
13+
recommend the Composer installation.
14+
- If you are thinking of contributing to the framework,
15+
then the Git installation is right for you.
1316

1417
.. toctree::
1518
:titlesonly:

user_guide_src/source/installation/troubleshooting.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ From the command line, at your project root::
1111

1212
php spark serve
1313

14-
``htt[://localhost:8080`` in your browser should then show the default
14+
``http://localhost:8080`` in your browser should then show the default
1515
welcome page:
1616

1717
|CodeIgniter4 Welcome|

user_guide_src/source/tutorial/conclusion.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
##########
21
Conclusion
3-
##########
2+
###############################################################################
43

54
This tutorial did not cover all of the things you might expect of a
65
full-fledged content management system, but it introduced you to the

user_guide_src/source/tutorial/create_news_items.rst

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
#################
21
Create news items
3-
#################
2+
###############################################################################
43

54
You now know how you can read data from a database using CodeIgniter, but
65
you haven't written any information to the database yet. In this section
76
you'll expand your news controller and model created earlier to include
87
this functionality.
98

109
Create a form
11-
-------------
10+
-------------------------------------------------------
1211

1312
To input data into the database you need to create a form where you can
1413
input the information to be stored. This means you'll be needing a form
1514
with two fields, one for the title and one for the text. You'll derive
1615
the slug from our title in the model. Create the new view at
17-
*app/Views/news/create.php*.
16+
**app/Views/news/create.php**.
1817

1918
::
2019

2120
<h2><?= esc($title); ?></h2>
2221

2322
<?= \Config\Services::validation()->listErrors(); ?>
2423

25-
<form>
24+
<form action="/news/create">
2625

2726
<label for="title">Title</label>
2827
<input type="input" name="title" /><br />
2928

30-
<label for="text">Text</label>
31-
<textarea name="text"></textarea><br />
29+
<label for="body">Text</label>
30+
<textarea name="body"></textarea><br />
3231

3332
<input type="submit" name="submit" value="Create news item" />
3433

3534
</form>
3635

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

40-
Go back to your news controller. You're going to do two things here,
40+
Go back to your ``News`` controller. You're going to do two things here,
4141
check whether the form was submitted and whether the submitted data
4242
passed the validation rules. You'll use the :doc:`form
4343
validation <../libraries/validation>` library to do this.
@@ -51,7 +51,7 @@ validation <../libraries/validation>` library to do this.
5151

5252
if (! $this->validate([
5353
'title' => 'required|min_length[3]|max_length[255]',
54-
'text' => 'required'
54+
'body' => 'required'
5555
]))
5656
{
5757
echo view('templates/header', ['title' => 'Create a news item']);
@@ -64,7 +64,7 @@ validation <../libraries/validation>` library to do this.
6464
$model->save([
6565
'title' => $this->request->getVar('title'),
6666
'slug' => url_title($this->request->getVar('title')),
67-
'text' => $this->request->getVar('text'),
67+
'body' => $this->request->getVar('body'),
6868
]);
6969
echo view('news/success');
7070
}
@@ -80,7 +80,7 @@ above. You can read :doc:`more about this library
8080
here <../libraries/validation>`.
8181

8282
Continuing down, you can see a condition that checks whether the form
83-
validation ran successfully. If it did not, the form is displayed, if it
83+
validation ran successfully. If it did not, the form is displayed; if it
8484
was submitted **and** passed all the rules, the model is called. This
8585
takes care of passing the news item into the model.
8686
This contains a new function, url\_title(). This function -
@@ -90,10 +90,14 @@ sure everything is in lowercase characters. This leaves you with a nice
9090
slug, perfect for creating URIs.
9191

9292
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.
93+
**app/Views/news/success.php** and write a success message.
9494

95-
Model
96-
-----
95+
This could be as simple as:::
96+
97+
News item created successfully.
98+
99+
Model Updating
100+
-------------------------------------------------------
97101

98102
The only thing that remains is ensuring that your model is setup
99103
to allow data to be saved properly. The ``save()`` method that was
@@ -109,14 +113,14 @@ fields in the ``$allowedFields`` property.
109113

110114
::
111115

112-
namespace App\Models;
116+
<?php namespace App\Models;
113117
use CodeIgniter\Model;
114118

115119
class NewsModel extends Model
116120
{
117121
protected $table = 'news';
118122

119-
protected $allowedFields = ['title', 'slug', 'text'];
123+
protected $allowedFields = ['title', 'slug', 'body'];
120124
}
121125

122126
This new property now contains the fields that we allow to be saved to the
@@ -126,10 +130,10 @@ This helps protect against Mass Assignment Vulnerabilities. If your model is
126130
handling your timestamps, you would also leave those out.
127131

128132
Routing
129-
-------
133+
-------------------------------------------------------
130134

131135
Before you can start adding news items into your CodeIgniter application
132-
you have to add an extra rule to *Config/Routes.php* file. Make sure your
136+
you have to add an extra rule to **app/Config/Routes.php** file. Make sure your
133137
file contains the following. This makes sure CodeIgniter sees 'create'
134138
as a method instead of a news item's slug.
135139

@@ -141,6 +145,28 @@ as a method instead of a news item's slug.
141145
$routes->get('(:any)', 'Pages::view/$1');
142146

143147
Now point your browser to your local development environment where you
144-
installed CodeIgniter and add index.php/news/create to the URL.
145-
Congratulations, you just created your first CodeIgniter application!
148+
installed CodeIgniter and add ``/news/create`` to the URL.
146149
Add some news and check out the different pages you made.
150+
151+
.. image:: ../images/tutorial3.png
152+
:align: center
153+
:height: 415px
154+
:width: 45%
155+
156+
.. image:: ../images/tutorial4.png
157+
:align: center
158+
:height: 415px
159+
:width: 45%
160+
161+
.. image:: ../images/tutorial9.png
162+
:align: left
163+
164+
165+
Congratulations
166+
-------------------------------------------------------
167+
168+
You just completed your first CodeIgniter4 application!
169+
170+
The image to the left shows your project's **app** folder,
171+
with all of the files that you created in green.
172+
The two modified configuration files (Database & Routes) are not shown.

0 commit comments

Comments
 (0)