Skip to content

Instantly share code, notes, and snippets.

@k1m0ch1
Last active June 7, 2022 01:08
Show Gist options
  • Save k1m0ch1/4947dbb75d202a9e4db0a35c44e10194 to your computer and use it in GitHub Desktop.
Save k1m0ch1/4947dbb75d202a9e4db0a35c44e10194 to your computer and use it in GitHub Desktop.
View Article
<?php
namespace Config;
// Create a new instance of our RouteCollection class.
$routes = Services::routes();
// Load the system's routing file first, so that the app and ENVIRONMENT
// can override as needed.
if (is_file(SYSTEMPATH . 'Config/Routes.php')) {
require SYSTEMPATH . 'Config/Routes.php';
}
/*
* --------------------------------------------------------------------
* Router Setup
* --------------------------------------------------------------------
*/
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
// The Auto Routing (Legacy) is very dangerous. It is easy to create vulnerable apps
// where controller filters or CSRF protection are bypassed.
// If you don't want to define all routes, please use the Auto Routing (Improved).
// Set `$autoRoutesImproved` to true in `app/Config/Feature.php` and set the following to true.
//$routes->setAutoRoute(false);
/*
* --------------------------------------------------------------------
* Route Definitions
* --------------------------------------------------------------------
*/
// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/', 'Home::index');
$routes->get('/halo', 'Home::intro');
$routes->get('/article', 'Article::index');
$routes->get('/article/(:any)', 'Article::show/$1');
/*
* --------------------------------------------------------------------
* Additional Routing
* --------------------------------------------------------------------
*
* There will often be times that you need additional routing and you
* need it to be able to override any defaults in this file. Environment
* based routes is one such time. require() additional route files here
* to make that happen.
*
* You will have access to the $routes object within that file without
* needing to reload it.
*/
if (is_file(APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php')) {
require APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php';
}
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
class Article extends Controller
{
public function __construct()
{
$this->article_model = new \App\Models\Article();
}
public function index()
{
// ambil artikel yang statusnya bukan draft
$data['articles'] = $this->article_model->get_published();
if (count($data['articles']->getResult()) > 0) {
// kirim data artikel ke view
return view('articles/list_article.php', $data);
} else {
// kalau gak ada artikel, tampilkan view ini
return view('articles/empty_article.php');
}
}
public function show($slug = null)
{
// jika gak ada slug di URL tampilkan 404
if (!$slug) {
return view('articles/empty_article.php');
}
// ambil artikel dengan slug yang diberikan
$data['article'] = $this->article_model->find_by_slug($slug);
// jika artikel tidak ditemuakn di database tampilkan 404
if (!$data['article']) {
$routes->set404Override('App\Errors::show404');
}
// tampilkan artikel
return view('articles/show_article.php', $data);
}
}
<?php
namespace App\Models;
use CodeIgniter\Model;
class Article extends Model
{
protected $table = 'article';
public function get_published($limit = null, $offset = null)
{
if (!$limit && $offset) {
$query = $this->getWhere(['draft' => 'false']);
} else {
$query = $this->getWhere(['draft' => 'false'], $limit, $offset);
}
return $query;
}
public function find_by_slug($slug)
{
if (!$slug) {
return;
}
$query = $this->getWhere(['slug' => $slug]);
return $query;
}
}
<h1>Tidak ada artikel</h1>
<h1>List Artikel</h1>
<ul>
<?php foreach ($articles->resultObject as $article) : ?>
<li>
<a href="<?= 'article/'.$article->slug ?>">
<?= $article->title ? $article->title : "No Title" ?>
</a>
</li>
<?php endforeach ?>
</ul>
<h1 class="post-title"><?= $article->getRowObject()->title ? $article->getRowObject()->title : "No Title" ?></h1>
<div class="post-meta">
Published at <?= $article->getRowObject()->created_at ?>
</div>
<div class="post-body">
<?= $article->getRowObject()->content ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment