1- #################
21Create news items
3- #################
2+ ###############################################################################
43
54You now know how you can read data from a database using CodeIgniter, but
65you haven't written any information to the database yet. In this section
76you'll expand your news controller and model created earlier to include
87this functionality.
98
109Create a form
11- -------------
10+ -------------------------------------------------------
1211
1312To input data into the database you need to create a form where you can
1413input the information to be stored. This means you'll be needing a form
1514with two fields, one for the title and one for the text. You'll derive
1615the 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
3838errors 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,
4141check whether the form was submitted and whether the submitted data
4242passed the validation rules. You'll use the :doc: `form
4343validation <../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
8080here <../libraries/validation>`.
8181
8282Continuing 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
8484was submitted **and ** passed all the rules, the model is called. This
8585takes care of passing the news item into the model.
8686This contains a new function, url\_ title(). This function -
@@ -90,10 +90,14 @@ sure everything is in lowercase characters. This leaves you with a nice
9090slug, perfect for creating URIs.
9191
9292After 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
98102The only thing that remains is ensuring that your model is setup
99103to 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
122126This 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
126130handling your timestamps, you would also leave those out.
127131
128132Routing
129- -------
133+ -------------------------------------------------------
130134
131135Before 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
133137file contains the following. This makes sure CodeIgniter sees 'create'
134138as 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
143147Now 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.
146149Add 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