BackendGuy

Creating a minimal blog in Codeigniter

Here we are going to learn about creating a minimal blog in codeigniter, but this our blog would have a homepage , so let’s say its a minimal codeigniter starter pack where you can visit the homepage, and then click on the blog link to navigate to the blog seection where you would see all the posts and also be able to click the link on a post and read along.

Create a controller that would control the models and the views for the minimal webpage which has a blog section, as we all know that codeigniter is a MVC framework, please if you really don’t know what MVC means, please lookup Here.

Lets call our site controller Pages, now it is also good to understand, that the name of our controller must be saved as the file name, so if my controller class is Pages, then i would save the file as Pages.php

Create the Pages controller

Go to the parent directory, and find the application directory, there you would find the controller folder, save the code below as Pages.php

<?php 

class Pages extends CI_Controller{
    function __construct(){
        parent::__construct();
        $this->load->helper('url_helper');
    }
    
    public function views($page = 'home'){
        if(! file_exists(APPPATH .'views/pages/'.$page.'.php')){
            show_404();
        }
        
        $data['title'] = ucfirst($page);
        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page,$data);
        $this->load->view('templates/footer', $data);
    }
}

?>

This code would help to display the header and footer in every page of the website which we would create, now from the code above we can see this code snippet

 if(! file_exists(APPPATH .'views/pages/'.$page.'.php')){ show_404(); }

This code snippet explains that if a page does not exist it should show the custom error page, that’s where the show_404() function comes in.

While the code below would make each page title have their first letter be in uppercase

$data['title'] = ucfirst($page);

The ucfirst() function does that, and please keep in mind that when we start creating pages, we would implement the title variable, if you don’t understand this then don’t worry, you’ll understand later

 $this->load->view('templates/header', $data);

 $this->load->view('pages/'.$page,$data);

 $this->load->view('templates/footer', $data);

These code snippets render the views which the user or visitor would see, codeigniter treats fragments of pages as one, but remember we have not yet created these views or rather pages, to do so, go to the application directory which should be inside your parent directory and find the views folder, then create the following file: home.php, remember when we created our view function we passed one argument to it called home, that’s the file we’re creating now.

<html>
<head>
<title> <?php echo $title; ?></title>
</head>
<body>
<h1> This is the homepage for the blogsite</h1>
</body>
</html>

Now inside the views directory, create a folder named template, now inside the template folder, create the header.php, and footer.php files.

views/templates/header.php
<html>
    <head>
        <title> <?php echo $title; ?></title>
    </head>

And for the footer it would be

views/templates/footer.php
<em> Copyright <?php echo date('Y'); ?></em>

Now we’re done with the first part, the next thing is to route these pages to render the desired view when our work is rendered either on localhost or using our domain, so find the config.php file and enter your custom routing code

$routes['default_controller'] = 'pages/views';
//where views is the function created in the Pages controller

$routes['(:any)'] = 'pages/views/$1';
//this code allows other pages to be rendered
//for instance our homepage would be : yoursite.ext/parent_directory

The parent directory is the folder where codeigniter was installed, and the page it would render would be home.php, which would certainly include our header and footer files, so the second routing code which has the (:any) tag, would enable us to navigate to other pages say for example : yoursite.ext/news.

So now, we are done with creating a demo homepage and enabling our url routing properly, the next thing is to create controllers and models and views that would enable us to set up a blog in this our already built demo site.

Now for the blog

Firstly, you need to create a database, and a table, but before that make sure you configure your database, find the config file and edit to your specs

$db['default'] = array(
        'dsn'   => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'database_name',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => TRUE,
        'db_debug' => TRUE,
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array()
);

Set up another controller class, call it news, and it should be in the same folder where you have the  pages controller.

<?php

class News extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->helper('url_helper');
$this->load->Model('News_Model');
}

public function index(){
$data['news'] = $this->news_model->get_news('news');
$data['title'] = 'News Archive';

//create the templates to be displayed
$this->load->view('templates/header',$data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer',$data);
}

//create the function to view single posts

public function views($slug = NULL){
$data[news_item] = $this->news_model->get_news($slug);
if(empty($data[news_item])){
show_404(); //show error if there is no post
}

$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header',$data);
$this->load->view('news/views',$data);
$this->load->view('templates/footer',$data);
}
}
?>

Now we have our controller for the blog section, we decided to call this class News, which you can always refer to as a blogging section or whatever, now we need to create a model which would pull the posts from the database to show our beloved viewers, now locate the models folder inside the application directory, and create a file, call it News_Model.php

<?php

class News_Model extends CI_Model{
function __construct(){
parent::__construct();
$this->load->database();
}

public function get_news($slug = FALSE){
if ($slug === FALSE){
$query = $this->db->get('news');
return $query->result_array();
}

$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
}
?>

Now we are done with the controllers and the models, now we need to show our viewers what we have done, remember the views folder? now get back to it, and create a folder named news. inside the news folder, create two files named index.php, and views.php

views/news/index.php

<?php

foreach($news as $key => $value){
echo $news['title'];
echo $news['text'];
echo "<p><a href="<?php echo site_url('news/'.$news['slug']); ?>">View article</a></p>";
}

?>
views/news/view.php

echo $news_item['title'];

 echo $news_item['text'];

Now the next thing is to update the routes

$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

Thanks alot for following through,My next post on creating a minimal blog on codeigniter would be on inserting posts by admin, besides i will be looking forward to accepting feedbacks,

BackendGuy

Add comment

Your Header Sidebar area is currently empty. Hurry up and add some widgets.