2
2
Bagian Berita
3
3
#############
4
4
5
- In the last section, we went over some basic concepts of the framework
6
- by writing a class that includes static pages. We cleaned up the URI by
7
- adding custom routing rules. Now it's time to introduce dynamic content
8
- and start using a database.
5
+ Pada bagian terakhir, kita mempelajari beberapa konsep dasar dari * framework *
6
+ dengan menulis sebuah * class * yang meliputi halaman statis. Kita membersihkan
7
+ URI dengan menambahkan * rule * routing kustom. Sekarang saatnya untuk mengenal
8
+ konten dinamis dan mulai menggunakan database.
9
9
10
- Setting up your model
10
+ Menyiapkan Model Anda
11
11
---------------------
12
12
13
- Instead of writing database operations right in the controller, queries
14
- should be placed in a model, so they can easily be reused later. Models
15
- are the place where you retrieve, insert, and update information in your
16
- database or other data stores. They represent your data.
13
+ Alih-alih menulis operasi * database * di controller, query seharusnya ditempatkan
14
+ dalam model, sehingga mereka dapat dengan mudah digunakan kembali nanti. * Model *
15
+ adalah tempat di mana Anda mengambil, menambahkan, dan memperbarui informasi
16
+ Anda di * database * atau menyimpan data lainnya. Mereka mewakili data Anda .
17
17
18
- Open up the *application/models/ * directory and create a new file called
19
- *News_model.php * and add the following code. Make sure you've configured
20
- your database properly as described :doc: `here <../database/configuration >`.
18
+ Buka direktori ``application/models/ `` dan buat file baru yang bernama
19
+ ``News_model.php `` dan tambahkan kode berikut. Pastikan Anda sudah
20
+ menkonfigurasi *database * Anda dengan benar seperti yang dijelaskan
21
+ :doc: `disini <../database/configuration >`.
21
22
22
23
::
23
24
24
25
<?php
25
- class News_model extends CI_Model {
26
+ class News_model extends CI_Model
27
+ {
26
28
27
29
public function __construct()
28
30
{
29
31
$this->load->database();
30
32
}
31
33
}
32
34
33
- This code looks similar to the controller code that was used earlier. It
34
- creates a new model by extending ``CI_Model `` and loads the database
35
- library. This will make the database class available through the
36
- ``$this->db `` object .
35
+ Kode ini terlihat mirip dengan kode controller yang digunakan sebelumnya. Kode
36
+ diatas menciptakan model baru dengan memperluas ``CI_Model `` dan memuat
37
+ * library database *. Ini akan membuat * class database* yang tersedia melalui
38
+ objek ``$this->db ``.
37
39
38
- Before querying the database, a database schema has to be created.
39
- Connect to your database and run the SQL command below (MySQL).
40
- Also add some seed records .
40
+ Sebelum melakukan * query * ke * database *, skema * database * harus dibuat terlebih
41
+ dahulu. Hubungkan ke * database * Anda dan jalankan perintah SQL di bawah ini
42
+ (MySQL). Yang juga akan menambahkan beberapa * record * .
41
43
42
44
::
43
45
@@ -50,20 +52,19 @@ Also add some seed records.
50
52
KEY slug (slug)
51
53
);
52
54
53
- Now that the database and a model have been set up, you'll need a method
54
- to get all of our posts from our database. To do this, the database
55
- abstraction layer that is included with CodeIgniter —
56
- :doc: ` Query Builder <../database/query_builder >` — is used. This makes it
57
- possible to write your 'queries' once and make them work on :doc: `all
58
- supported database systems <../general/requirements>`. Add the
59
- following code to your model.
55
+ Sekarang * database * dan * model * telah diatur, Anda akan memerlukan * method *
56
+ untuk mendapatkan semua * posting * kita dari database. Untuk melakukan hal ini,
57
+ * database abstraction layer* yang disertakan dengan CodeIgniter - :doc: ` Query
58
+ Builder <../database/query_builder>` - digunakan. Hal ini memungkinkan Anda
59
+ untuk menulis '* query *' sekali dan membuat mereka bekerja pada :doc: `semua
60
+ sistem database didukung <../general/requirements>`. Tambahkan kode berikut
61
+ untuk * model * Anda .
60
62
61
63
::
62
64
63
65
public function get_news($slug = FALSE)
64
66
{
65
- if ($slug === FALSE)
66
- {
67
+ if ($slug === FALSE) {
67
68
$query = $this->db->get('news');
68
69
return $query->result_array();
69
70
}
@@ -72,24 +73,26 @@ following code to your model.
72
73
return $query->row_array();
73
74
}
74
75
75
- With this code you can perform two different queries. You can get all
76
- news records, or get a news item by its `slug <# >`_. You might have
77
- noticed that the ``$slug `` variable wasn't sanitized before running the
78
- query; :doc: `Query Builder <../database/query_builder >` does this for you.
76
+ Dengan kode ini Anda dapat melakukan dua *query * yang berbeda. Anda bisa
77
+ mendapatkan semua *record * berita, atau mendapatkan sebuah berita dengan `slug
78
+ <#> `_. Anda mungkin melihat bahwa variabel ``$slug `` tidak dibersihkan sebelum
79
+ menjalankan query*. :doc: `Query Builder <../database/query_builder >`
80
+ melakukannya untuk anda.
79
81
80
- Display the news
81
- ----------------
82
+ Menampilkan Berita
83
+ ------------------
82
84
83
- Now that the queries are written, the model should be tied to the views
84
- that are going to display the news items to the user. This could be done
85
- in our ``Pages `` controller created earlier, but for the sake of clarity ,
86
- a new ``News `` controller is defined. Create the new controller at
87
- * application/controllers/News.php * .
85
+ Sekarang * query * sudah ditulis, * model * harus terikat dengan * view *
86
+ yang akan menampilkan berita kepada pengguna. Hal ini dapat dilakukan
87
+ di * controller * ``Pages `` yang kita buat sebelumnya, tapi demi kejelasan ,
88
+ * controller * ``News `` baru telah didefinisikan. Buat * controller * baru di
89
+ `` application/controllers/News.php `` .
88
90
89
91
::
90
92
91
93
<?php
92
- class News extends CI_Controller {
94
+ class News extends CI_Controller
95
+ {
93
96
94
97
public function __construct()
95
98
{
@@ -109,21 +112,21 @@ a new ``News`` controller is defined. Create the new controller at
109
112
}
110
113
}
111
114
112
- Looking at the code, you may see some similarity with the files we
113
- created earlier. First, the ``__construct() `` method: it calls the
114
- constructor of its parent class (``CI_Controller ``) and loads the model,
115
- so it can be used in all other methods in this controller.
116
- It also loads a collection of :doc: `URL Helper <../helpers/url_helper >`
117
- functions, because we'll use one of them in a view later .
115
+ Melihat kode diatas, Anda dapat melihat beberapa kesamaan dengan file yang kita
116
+ buat sebelumnya. Pertama, * method * ``__construct() ``: itu memanggil konstruktor
117
+ * class * induknya (``CI_Controller ``) dan memuat * model *, sehingga dapat
118
+ digunakan di semua * method * di dalam * controller * ini. Hal ini juga memuat
119
+ * collection * dari fungsi :doc: `URL Helper <../helpers/url_helper >`, karena kita
120
+ akan menggunakan salah satu dari mereka dalam * view * nanti .
118
121
119
- Next, there are two methods to view all news items and one for a specific
120
- news item. You can see that the ``$slug `` variable is passed to the model's
121
- method in the second method. The model is using this slug to identify the
122
- news item to be returned .
122
+ Berikutnya, ada dua * method * untuk melihat semua berita dan satu berita untuk
123
+ berita tertentu. Anda dapat melihat bahwa variabel ``$slug `` dilewatkan ke
124
+ * method * * model * dalam * method * kedua. * Model * ini menggunakan * slug * untuk
125
+ mengidentifikasi berita yang dikembalikan .
123
126
124
- Now the data is retrieved by the controller through our model, but
125
- nothing is displayed yet. The next thing to do is passing this data to
126
- the views .
127
+ Sekarang data tersebut diambil oleh * controller * melalui * model * kami, tapi
128
+ belum ada yang ditampilkan. Hal berikutnya yang harus dilakukan adalah
129
+ mengoper (* passing *) data ini ke * view * .
127
130
128
131
::
129
132
@@ -137,11 +140,11 @@ the views.
137
140
$this->load->view('templates/footer');
138
141
}
139
142
140
- The code above gets all news records from the model and assigns it to a
141
- variable. The value for the title is also assigned to the `` $data['title'] ``
142
- element and all data is passed to the views. You now need to create a
143
- view to render the news items. Create * application/views/news/index.php *
144
- and add the next piece of code .
143
+ Kode di atas berfungsi untuk mendapat semua * record * berita dari * model * dan
144
+ * assign * ke sebuah variabel. Nilai untuk judul juga di-* assign * ke elemen
145
+ `` $data['title'] `` dan semua data akan dioper ke * view *. Anda sekarang perlu
146
+ untuk membuat * view * untuk memuat berita. Buat
147
+ `` application/views/news/index.php `` dan tambahkan potongan kode berikut .
145
148
146
149
::
147
150
@@ -157,16 +160,17 @@ and add the next piece of code.
157
160
158
161
<?php endforeach; ?>
159
162
160
- Here, each news item is looped and displayed to the user. You can see we
161
- wrote our template in PHP mixed with HTML. If you prefer to use a template
162
- language, you can use CodeIgniter's :doc: `Template
163
- Parser <../libraries/parser>` class or a third party parser.
163
+ Di sini, setiap item berita diulang dan ditampilkan kepada pengguna. Anda dapat
164
+ melihat kita menulis *template * kita di PHP yang dicampur dengan HTML. Jika
165
+ Anda memilih untuk menggunakan *template language *, Anda dapat menggunakan
166
+ CodeIgniter *class * :doc: `Template Parser <../libraries/parser >` atau
167
+ *parser * pihak ketiga.
164
168
165
- The news overview page is now done, but a page to display individual
166
- news items is still absent. The model created earlier is made in such
167
- way that it can easily be used for this functionality. You only need to
168
- add some code to the controller and create a new view. Go back to the
169
- ``News `` controller and update ``view() `` with the following :
169
+ Halaman ikhtisar berita sekarang selesai, tetapi halaman untuk menampilkan
170
+ berita individu masih belum. * Model * yang dibuat sebelumnya dibuat sedemikian
171
+ rupa agar mudah digunakan untuk fungsi ini. Anda hanya perlu menambahkan
172
+ beberapa kode untuk * controller * dan membuat * view * baru. Kembali ke
173
+ * controller * ``News `` dan perbaiki * method * ``view() `` seperti berikut :
170
174
171
175
::
172
176
@@ -186,10 +190,10 @@ add some code to the controller and create a new view. Go back to the
186
190
$this->load->view('templates/footer');
187
191
}
188
192
189
- Instead of calling the ``get_news() `` method without a parameter, the
190
- `` $slug `` variable is passed, so it will return the specific news item.
191
- The only things left to do is create the corresponding view at
192
- * application/views/news/view.php *. Put the following code in this file.
193
+ Alih-alih memanggil * method * ``get_news() `` tanpa parameter, variabel `` $slug ``
194
+ dioper, sehingga akan mengembalikan * item * berita tertentu. Satu-satunya hal
195
+ yang tersisa untuk dilakukan adalah membuat * view * yang sesuai di
196
+ `` application/views/news/view.php ``. Masukan kode berikut dalam file ini .
193
197
194
198
::
195
199
@@ -200,12 +204,12 @@ The only things left to do is create the corresponding view at
200
204
Routing
201
205
-------
202
206
203
- Because of the wildcard routing rule created earlier, you need an extra
204
- route to view the controller that you just made. Modify your routing file
205
- (* application/config/routes.php *) so it looks as follows .
206
- This makes sure the requests reaches the ``News `` controller instead of
207
- going directly to the ``Pages `` controller. The first line routes URI's
208
- with a slug to the ``view() `` method in the ``News `` controller .
207
+ Karena * rule routing wildcard* yang kita buat sebelumnya, Anda perlu route*
208
+ ekstra untuk melihat * controller * yang baru saja Anda buat. Modifikasi file
209
+ * routing * (`` application/config/routes.php ``) sehingga terlihat sebagai berikut .
210
+ Hal ini untuk memastikan permintaan mencapai * controller * ``News `` bukannya
211
+ langsung ke * controller * ``Pages ``. * Route * URI baris pertama dengan * slug * ke
212
+ * method * ``view() `` dalam * controller * ``News ``.
209
213
210
214
::
211
215
@@ -214,5 +218,5 @@ with a slug to the ``view()`` method in the ``News`` controller.
214
218
$route['(:any)'] = 'pages/view/$1';
215
219
$route['default_controller'] = 'pages/view';
216
220
217
- Point your browser to your document root, followed by index.php/news and
218
- watch your news page .
221
+ Arahkan browser Anda ke * root * dokumen Anda, diikuti dengan `` index.php/news ``
222
+ dan lihat halaman berita Anda .
0 commit comments