Langkah 7: Buat Controller
Sekarang mari kita membuat kontroler pertama kami. Ingat konvensi ini.
- kontroler nama file harus huruf kecil, misalnya album.php
-
kelas kontroler harus memetakan dengan nama file dan dikapitalisasi,
dan harus ditambahkan dengan _Controller, misalnya Album_Controller - harus memiliki kelas Controller sebagai (grand) orang tua
Juga, ingat bagaimana Kohana struktur URL dan bagaimana Anda dapat memanggil metode pengendali; misalnya http: //hostname/kohana_directory/index.php/controller/function.
Mari kita lihat kontroler yang sederhana ini.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" ><?php defined( 'SYSPATH' ) OR die ( 'No direct access allowed.' );</span> <? Php didefinisikan ( 'SYSPATH' ) ATAU mati ( 'Tidak ada akses langsung diperbolehkan.' );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > class Album_Controller extends Controller</span> kelas Album_Controller meluas Pengendali</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > public function __construct()</span> fungsi publik __construct ()</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >parent::__construct();</span> parent :: __ construct ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > public function index()</span> Indeks fungsi publik ()</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > echo "My first controller" ;</span> echo "Saya kontroler pertama" ;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> |
PHP5 OOP adalah prasyarat. Jadi jika Anda tidak berpengalaman, Anda dapat mempelajari lebih lanjut di sini .
Fungsi konstruktor, disebut __construct, menginisialisasi kelas dan memanggil konstruktor induk.
Fungsi indeks fungsi default, sehingga akan dipanggil jika kita sebut controller tanpa
menentukan fungsi apapun (misalnya http:. //localhost/index.php/kohana/album Setelah nama kontroler
tidak ada fungsi apapun, fungsi indeks default akan dipanggil.)
Mengingat aturan-aturan dasar, mari kita fokus pada aplikasi kita. Album kontroler mengimplementasikan semua tindakan untuk
pengelolaan koleksi album. Kontroler ini memungkinkan kita untuk membuat album baru, untuk menunjukkan album yang tersimpan dalam database kami,
untuk memperbarui album, dan menghapus album.
Jadi, mari kita mengubah kelas sebagai berikut.
Buat file bernama album.php dalam aplikasi / controllers / paste berikut.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" ><?php defined( 'SYSPATH' ) OR die ( 'No direct access allowed.' );</span> <? Php didefinisikan ( 'SYSPATH' ) ATAU mati ( 'Tidak ada akses langsung diperbolehkan.' );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > class Album_Controller extends Controller</span> kelas Album_Controller meluas Pengendali</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private $album_model ;</span> $ album_model swasta;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private $genre_model ;</span> $ genre_model swasta;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private $list_view ;</span> $ list_view swasta;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private $create_view ;</span> $ create_view swasta;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private $update_view ;</span> $ update_view swasta;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > public function __construct()</span> fungsi publik __construct ()</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >parent::__construct();</span> parent :: __ construct ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->album_model = new Album_Model;</span> $ This-> album_model = Album_Model baru;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->genre_model = new Genre_Model;</span> $ This-> genre_model = baru Genre_Model;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->list_view = new View( 'list' );</span> $ This-> list_view = new View ( 'list' );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->update_view = new View( 'update' );</span> $ This-> update_view = new View ( 'update' );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->create_view = new View( 'create' );</span> $ This-> create_view = new View ( 'membuat' );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > public function index()</span> Indeks fungsi publik ()</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->show_albums_list();</span> $ This-> show_albums_list ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private function show_albums_list()</span> fungsi swasta show_albums_list ()</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $albums_list = $this ->album_model->get_list();</span> $ Albums_list = $ this-> album_model-> get_list ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->list_view->set( 'albums_list' , $albums_list );</span> $ This-> list_view-> set ( 'albums_list' , $ albums_list);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->list_view->render(TRUE);</span> $ This-> list_view-> render (TRUE);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > public function show_create_editor()</span> fungsi publik show_create_editor ()</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->create_view->set( 'genres_list' , $this ->get_genres_list());</span> $ This-> create_view-> set ( 'genres_list' , $ this-> get_genres_list ());</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->create_view->render(TRUE);</span> $ This-> create_view-> render (TRUE);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > public function show_update_editor( $id )</span> fungsi publik show_update_editor ($ id)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $album_data = $this ->album_model->read( $id );</span> $ Album_data = $ this-> album_model-> baca ($ id);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->update_view->set( 'album_id' , $album_data [0]->id);</span> $ This-> update_view-> set ( 'album_id' , $ album_data [0] -> id);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->update_view->set( 'name' , $album_data [0]->name);</span> $ This-> update_view-> set ( 'nama' , $ album_data [0] -> nama);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->update_view->set( 'author' , $album_data [0]->author);</span> $ This-> update_view-> set ( 'penulis' , $ album_data [0] -> penulis);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->update_view->set( 'genre_id' , $album_data [0]->genre_id);</span> $ This-> update_view-> set ( 'genre_id' , $ album_data [0] -> genre_id);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->update_view->set( 'genres_list' , $this ->get_genres_list());</span> $ This-> update_view-> set ( 'genres_list' , $ this-> get_genres_list ());</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->update_view->render(TRUE);</span> $ This-> update_view-> render (TRUE);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > public function create()</span> fungsi publik membuat ()</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $album_data = array (</span> $ Album_data = array (</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'name' => $this ->input->post( 'name' ),</span> 'Nama' => $ this-> input-> pasca ( 'nama' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'author' => $this ->input->post( 'author' ),</span> 'Penulis' => $ this-> input-> pasca ( 'penulis' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'genre_id' => $this ->input->post( 'genre_id' )</span> 'Genre_id' => $ this-> input-> pasca ( 'genre_id' )</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >);</span> );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->album_model->create( $album_data );</span> $ This-> album_model-> membuat (album_data $);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >url::redirect( 'album' );</span> url :: redirect ( 'album' );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > public function update()</span> pembaruan fungsi publik ()</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $album_data = array (</span> $ Album_data = array (</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'name' => $this ->input->post( 'name' ),</span> 'Nama' => $ this-> input-> pasca ( 'nama' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'author' => $this ->input->post( 'author' ),</span> 'Penulis' => $ this-> input-> pasca ( 'penulis' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'genre_id' => $this ->input->post( 'genre_id' )</span> 'Genre_id' => $ this-> input-> pasca ( 'genre_id' )</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >);</span> );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->album_model->update( $this ->input->post( 'album_id' ), $album_data );</span> $ This-> album_model-> update ($ this-> input-> pasca ( 'album_id' ), $ album_data);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >url::redirect( 'album' );</span> url :: redirect ( 'album' );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > public function delete ( $id )</span> fungsi publik menghapus ($ id)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->album_model-> delete ( $id );</span> $ This-> album_model-> menghapus ($ id);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >url::redirect( 'album' );</span> url :: redirect ( 'album' );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private function get_genres_list()</span> fungsi swasta get_genres_list ()</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $db_genres_list = $this ->genre_model->get_list();</span> $ Db_genres_list = $ this-> genre_model-> get_list ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $genres_list = array ();</span> $ Genres_list = array ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > if (sizeof( $db_genres_list ) >= 1)</span> jika (sizeof ($ db_genres_list)> = 1)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > foreach ( $db_genres_list as $item )</span> foreach ($ db_genres_list sebagai $ item)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $genres_list [ $item ->id] = $item ->name;</span> $ Genres_list [$ item-> id] = $ item-> nama;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > return $genres_list ;</span> kembali $ genres_list;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> |
Mari saya jelaskan apa kode ini tidak.
Lima anggota variabel dideklarasikan di bagian atas kelas:
1 2 3 4 5 6 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private $album_model ;</span> $ album_model swasta;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private $genre_model ;</span> $ genre_model swasta;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private $list_view ;</span> $ list_view swasta;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private $create_view ;</span> $ create_view swasta;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private $update_view ;</span> $ update_view swasta;</span> |
Anggota adalah pribadi karena saya ingin membatasi visibilitas hanya untuk kelas ini.
Dalam metode membangun model dan melihat benda-benda yang dibuat menggunakan lima anggota:
1 2 3 4 5 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->album_model = new Album_Model;</span> $ This-> album_model = Album_Model baru;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->genre_model = new Genre_Model;</span> $ This-> genre_model = baru Genre_Model;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->list_view = new View( 'list' );</span> $ This-> list_view = new View ( 'list' );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->update_view = new View( 'update' );</span> $ This-> update_view = new View ( 'update' );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->create_view = new View( 'create' );</span> $ This-> create_view = new View ( 'membuat' );</span> |
Untuk membuat penggunaan objek model sintaks ini:
1 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $obj_name = new Name_Model;</span> $ Obj_name = Name_Model baru;</span> |
Untuk membuat objek pandang, menggunakan sintaks ini:
1 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $obj_name = new View( 'view_filename_without_extension' );</span> $ Obj_name = new View ( 'view_filename_without_extension' );</span> |
Sekarang ada dua benda untuk mengakses album dan genre model, dan tiga
objek untuk mengakses pandangan yang dibutuhkan untuk membuat
presentasi.
Metode Indeks memanggil metode show_albums_list yang berisi daftar semua album yang tersimpan dalam database.
1 2 3 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $albums_list = $this ->album_model->get_list();</span> $ Albums_list = $ this-> album_model-> get_list ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->list_view->set( 'albums_list' , $albums_list );</span> $ This-> list_view-> set ( 'albums_list' , $ albums_list);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->list_view->render(TRUE);</span> $ This-> list_view-> render (TRUE);</span> |
Dalam metode ini Anda dapat melihat bagaimana model dan melihat objek yang digunakan untuk mengakses metode relatif. "Get_list" adalah metode model yang (kita akan lihat nanti) yang mengembalikan semua album yang disimpan dalam database. Hasilnya disimpan di "$ album_list" array. Untuk lulus hasil array yang dari controller untuk pandangan, "mengatur" metode ini disebut pada objek pandangan. Metode ini membutuhkan dua parameter: variabel kosong baru (album_list) mengandung data yang ada variabel ($ album_list).
Sekarang variabel baru "album_list" berisi array $ album_list (kita
akan lihat nanti bagaimana menampilkan konten dalam tampilan). Metode "membuat", dengan parameter TRUE, diperlukan data output ke browser.
Metode show_create_editor menunjukkan antarmuka pengguna untuk memasukkan album baru.
1 2 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->create_view->set( 'genres_list' , $this ->get_genres_list());</span> $ This-> create_view-> set ( 'genres_list' , $ this-> get_genres_list ());</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->create_view->render(TRUE);</span> $ This-> create_view-> render (TRUE);</span> |
Daftar genre dilewatkan ke tampilan.
Metode show_update_editor menunjukkan antarmuka pengguna untuk memperbarui album yang sudah ada.
1 2 3 4 5 6 7 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $album_data = $this ->album_model->read( $id );</span> $ Album_data = $ this-> album_model-> baca ($ id);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->update_view->set( 'album_id' , $album_data [0]->id);</span> $ This-> update_view-> set ( 'album_id' , $ album_data [0] -> id);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->update_view->set( 'name' , $album_data [0]->name);</span> $ This-> update_view-> set ( 'nama' , $ album_data [0] -> nama);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->update_view->set( 'author' , $album_data [0]->author);</span> $ This-> update_view-> set ( 'penulis' , $ album_data [0] -> penulis);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->update_view->set( 'genre_id' , $album_data [0]->genre_id);</span> $ This-> update_view-> set ( 'genre_id' , $ album_data [0] -> genre_id);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->update_view->set( 'genres_list' , $this ->get_genres_list());</span> $ This-> update_view-> set ( 'genres_list' , $ this-> get_genres_list ());</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->update_view->render(TRUE);</span> $ This-> update_view-> render (TRUE);</span> |
"Membaca" adalah metode model yang (kita akan lihat nanti) yang
mengembalikan data ($ album_data) dari album dengan id sama dengan $ id. Kemudian, setiap elemen tunggal dari album data yang dikembalikan akan diteruskan ke tampilan.
Menciptakan metode menerima data, untuk album baru, dari pandangan dan data disimpan dalam database.
1 2 3 4 5 6 7 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $album_data = array (</span> $ Album_data = array (</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'name' => $this ->input->post( 'name' ),</span> 'Nama' => $ this-> input-> pasca ( 'nama' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'author' => $this ->input->post( 'author' ),</span> 'Penulis' => $ this-> input-> pasca ( 'penulis' ),</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 'genre_id' => $this ->input->post( 'genre_id' )</span> 'Genre_id' => $ this-> input-> pasca ( 'genre_id' )</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >);</span> );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $this ->album_model->create( $album_data );</span> $ This-> album_model-> membuat (album_data $);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >url::redirect( 'album' );</span> url :: redirect ( 'album' );</span> |
$ Album_data adalah array yang berisi data POST dari pandangan. Untuk menyimpan album, array akan diteruskan ke membuat model yang metode. Baris terakhir adalah panggilan untuk metode pembantu. Pembantu hanya fungsi yang membantu Anda dengan pembangunan. Kelas pembantu secara otomatis dimuat oleh framework. Pembantu dinyatakan sebagai metode statis kelas, sehingga tidak perlu untuk instantiate kelas.
Dalam hal ini metode "mengarahkan" dari pembantu "url" disebut dan
mengatakan Kohana untuk mengarahkan browser ke controller album. Hal ini untuk menghindari memasukkan baru (misalnya menekan F5).
"Pembantu hanya fungsi yang membantu Anda dengan pembangunan."
Update dan menghapus metode bekerja dengan cara yang sama sebagai metode membuat atas.
Metode
terakhir get_genres_list mendapatkan daftar genre dari model ($
db_genres_list) dan membangun sebuah array baru ($ genres_list) untuk
kotak pilih dalam pandangan.
01 02 03 04 05 06 07 08 09 10 11 | <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $db_genres_list = $this ->genre_model->get_list();</span> $ Db_genres_list = $ this-> genre_model-> get_list ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $genres_list = array ();</span> $ Genres_list = array ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > if (sizeof( $db_genres_list ) >= 1)</span> jika (sizeof ($ db_genres_list)> = 1)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > foreach ( $db_genres_list as $item )</span> foreach ($ db_genres_list sebagai $ item)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > $genres_list [ $item ->id] = $item ->name;</span> $ Genres_list [$ item-> id] = $ item-> nama;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > return $genres_list ;</span> kembali $ genres_list;</span> |
Kohana: The Swift PHP Framework Langkah 8: Buat Model Project
Kohana: The Swift PHP Framework
Kohana: The Swift PHP Framework Langkah 10: Final Thoughts
Kohana: The Swift PHP Framework Langkah 9: Buat Proyek View
Kohana: The Swift PHP Framework Langkah 8: Buat Model Project
Kohana: The Swift PHP Framework Langkah 7: Buat Controller
Kohana: The Swift PHP Framework Langkah 6: Database Proyek
Kohana: The Swift PHP Framework Langkah 5: pertama Kohana Proyek Anda
Kohana: The Swift PHP Framework Langkah 4: Konfigurasi Kohana
Kohana: The Swift PHP Framework Langkah 3: Instalasi Kohana
Kohana: The Swift PHP Framework Langkah 2: Men-download Kohana
Kohana: The Swift PHP Framework Langkah 1: Apa Kohana?
0 komentar:
Posting Komentar
silahkan komentar