@@ -6,6 +6,20 @@ you haven't written any information to the database yet. In this section,
66you'll expand your news controller and model created earlier to include
77this functionality.
88
9+ Enable CSRF Filter
10+ ------------------
11+
12+ Before creating a form, let's enable the CSRF protection.
13+
14+ Open the **app/Config/Filters.php ** file and update the ``$methods `` property like the following::
15+
16+ public $methods = [
17+ 'post' => ['csrf'],
18+ ];
19+
20+ It configures the CSRF filter to be enabled for all **POST ** requests.
21+ You can read more about the CSRF protection in :doc: `Security </libraries/security >` library.
22+
923Create a form
1024-------------
1125
@@ -19,7 +33,7 @@ the slug from our title in the model. Create a new view at
1933
2034 <h2><?= esc($title) ?></h2>
2135
22- <?= \Config\Services::validation( )->listErrors() ?>
36+ <?= service('validation' )->listErrors() ?>
2337
2438 <form action="/news/create" method="post">
2539 <?= csrf_field() ?>
@@ -28,13 +42,13 @@ the slug from our title in the model. Create a new view at
2842 <input type="input" name="title" /><br />
2943
3044 <label for="body">Text</label>
31- <textarea name="body"></textarea><br />
45+ <textarea name="body" cols="45" rows="4" ></textarea><br />
3246
3347 <input type="submit" name="submit" value="Create news item" />
3448 </form>
3549
3650There are probably only two things here that look unfamiliar. The
37- ``\Config\Services::validation( )->listErrors() `` function is used to report
51+ ``service('validation' )->listErrors() `` function is used to report
3852errors related to form validation. The ``csrf_field() `` function creates
3953a hidden input with a CSRF token that helps protect against some common attacks.
4054
@@ -47,7 +61,7 @@ validation <../libraries/validation>` library to do this.
4761
4862 public function create()
4963 {
50- $model = new NewsModel( );
64+ $model = model(NewsModel::class );
5165
5266 if ($this->request->getMethod() === 'post' && $this->validate([
5367 'title' => 'required|min_length[3]|max_length[255]',
@@ -60,7 +74,6 @@ validation <../libraries/validation>` library to do this.
6074 ]);
6175
6276 echo view('news/success');
63-
6477 } else {
6578 echo view('templates/header', ['title' => 'Create a news item']);
6679 echo view('news/create');
@@ -69,9 +82,9 @@ validation <../libraries/validation>` library to do this.
6982 }
7083
7184The code above adds a lot of functionality. First we load the NewsModel.
72- After that, we check if we deal with the `` POST `` request and then
85+ After that, we check if we deal with the ** POST ** request and then
7386the Controller-provided helper function is used to validate
74- the $_POST fields . In this case, the title and text fields are required.
87+ the user input data . In this case, the POST data, and the title and text fields are required.
7588
7689CodeIgniter has a powerful validation library as demonstrated
7790above. You can read :doc: `more about this library
@@ -83,7 +96,7 @@ was submitted **and** passed all the rules, the model is called. This
8396takes care of passing the news item into the model.
8497This contains a new function ``url_title() ``. This function -
8598provided by the :doc: `URL helper <../helpers/url_helper >` - strips down
86- the string you pass it, replacing all spaces by dashes (- ) and makes
99+ the string you pass it, replacing all spaces by dashes (`` - `` ) and makes
87100sure everything is in lowercase characters. This leaves you with a nice
88101slug, perfect for creating URIs.
89102
@@ -106,9 +119,9 @@ or if the row already exists and should be updated, based on the presence
106119of a primary key. In this case, there is no ``id `` field passed to it,
107120so it will insert a new row into it's table, **news **.
108121
109- However, by default the insert and update methods in the model will
122+ However, by default the insert and update methods in the Model will
110123not actually save any data because it doesn't know what fields are
111- safe to be updated. Edit the model to provide it a list of updatable
124+ safe to be updated. Edit the ** NewsModel ** to provide it a list of updatable
112125fields in the ``$allowedFields `` property.
113126
114127::
@@ -168,8 +181,8 @@ Congratulations
168181You just completed your first CodeIgniter4 application!
169182
170183The image underneath 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.
184+ with all of the files that you created in red .
185+ The two modified configuration files (** Config/Routes.php ** & ** Config/Filters.php ** ) are not shown.
173186
174187.. image :: ../images/tutorial9.png
175188 :align: left
0 commit comments