Hai, bali lagi ditutorial pemrograman web Dumet School kali ini saya akan menjelaskan tentang Bagaimana Cara Membuat Halaman Dinamis Menggunakan CodeIgniter, pada artikel sebelumnya kita sudah membuat title dinamis menggunakan php procedural, nah kali ini kita akan membuatnya menggunakan framework CodeIgniter, lebih menantang yah hehe ..
tapi perlu kalian tahu menggunakan CodeIgniter malah lebih mudah dibandingkan dengan php procedural yang biasa teman teman gunakan, penasaran mudahnya gimana? Yu langsung saja kita mulai praktekan, terlebih dahulu teman teman download file codeigniternya di codeigniter.com website resminya, jika sudah didownload teman teman setting yah, jika teman teman bingung cara settingnya bisa baca-baca terlebih dahulu artikel saya sebelumnya tentang codeigniter, saya asumsikan teman teman sudah paham yah, oke hal yang perlu dilakukan ialah kita akan membuat 3 buah file controller dan 1 file view beri nama file tersebut, Home.php, Profile.php, Contact.php dan pada folder view buatkan folder baru bernama template lalu didalamnya buatkan file bernama index.php . sekarang coba tambahkan script berikut pada controller Home.php
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php class Home extends CI_Controller { public function index(){ $data['title'] = 'Halaman Home'; $data['page'] = 'Halaman Home'; $data['content'] = ' Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum s that it has a more-or-less normal distribution of letters, as opposed o using Content here, content here, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). '; $this->load->view('template/index', $data); } } |
Dan Profile.php
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php class Profile extends CI_Controller { public function index(){ $data['title'] = 'Halaman Profile'; $data['page'] = 'Halaman Profile'; $data['content'] = ' What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem psum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. '; $this->load->view('template/index', $data); } } |
Contact.php
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php class Contact extends CI_Controller { public function index(){ $data['title'] = 'Halaman Contact'; $data['page'] = 'Halaman Contact'; $data['content'] = ' Where does it come from? Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 ars old. Richard McClintock, a Latin professor at Hampden-Sydney College in irginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem psum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. '; $this->load->view('template/index', $data); } } |
Dan view didalam folder template/index.php
|
1 2 3 4 5 6 7 8 9 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 |
<!DOCTYPE html> <html> <head> <title><?php echo $title ?></title> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="description" content="Demo project with jQuery"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous"> <style type="text/css"></style> </head> <body> <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <a class="navbar-brand" href="#">Navbar</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav"> <li class="nav-item active"> <a class="nav-link" href="<?php echo site_url('home') ?>">Home</a> </li> <li class="nav-item active"> <a class="nav-link" href="<?php echo site_url('profile') ?>">Profile</a> </li> <li class="nav-item active"> <a class="nav-link" href="<?php echo site_url('contact') ?>">Contact</a> </li> </ul> </div> </nav> <div class="container"> <div class="row"> <div class="col-md-12"> <div class="jumbotron"> <h1> Hallo, <?= $page ?> </h1> <p><?= $content ?></p> </div> </div> </div> </div> <script src="http://code.jquery.com/jquery-latest.min.js"></script> <script type="text/javascript"> jQuery(function(){ }); </script> </body> </html> |
Semuanya disave dan jalankan dibrowsernya dengan memanggil localhost/namafolder/index.php/Home enter coba klik disetaip menunya maka secara otomatis title dan halaman disesuaikan dengan file yang kita klik, oke cukup mudah bukan?, sekian tutorial tentang Bagaimana Cara Membuat Halaman Dinamis Menggunakan CodeIgniter. Sampai bertemu diartikel selanjutnya dan sampai jumpa.
#KeepLearn
#IsmetMA