Create a Simple Blog System in Codeigniter4 Framework

 There are many php frameworks out there like Laravel, kohana, Codeigniter, and some micro-frameworks e.g slim, lumen..etc.

Now a days Laravel is the most used php framework it there. From creating a blog to Enterprise level web applications.

We will discuss here about Codeigniter4 which is a php based framework with a little footprint. It's faster than Laravel.

Let's go.....


First of all if you are using windows then download xampp server that's the only thing you need. 

Step:2

Go to Codeigniter.com and download the latest version of it. Or you can download it via composer. 

Extract the file into your htdocs folder. Then run XAMPP server. Now go to localhost/<nameofyourwebsite>folder/publicyou will see Codeigniter4 welcome page.

Well done ✅ you have successfully installed the basic setup. 


Now the main part is how we can create our page (home,about,contact,...etc). 

CONTROLLER:

Create or just copy the home controller and rename it to PagesController.php. That means each and every page will serve by this controller.


Now in the views section create a folder name pages which will contain all of our pages.

set routes to -> 

$routes->get('(:any)''Pages::view/$1');

now open your browser and search for any page ..like

http://localhost/myblog/about

http://localhost/myblog/contact

http://localhost/myblog/downloads

Before that you have to create such pages in your views/pages folder. Otherwise it will throw an error.

Now let's create our Contact us Model (database)

Configure your Database settings in the .env file----

The file something looks like this just remove the hash(uncomment) this. and set your database name, username and password(if you have any otherwise leave it blank)


# database.default.hostname = localhost

# database.default.database = myblog

# database.default.username = root

# database.default.password = 

# database.default.DBDriver = MySQLi


Model for our contact us page

<?php namespace App\Models;

use CodeIgniter\Model;

class ContactModel extends Model{
  protected $table = 'contact';
  protected $allowedFields = ['name''email''query'];
  

}

Create a Controller for our ContactUs page i.e ContactUs.php and import the ContactModel in the contactus controller.


Now Let's Do CRUD operation i.e how we can create a post ,edit the post and delete the post and update the post and create seo(search engine optimization url). 

CREATE POST: 

Make a model for blog... BlogModel.php

     
   <?php namespace App\Models;

        use CodeIgniter\Model;

        class BlogModel extends Model{
            protected $table = 'posts';
        

            protected $allowedFields = ['title''slug''body','description'];

            public function getPosts($slug = null){
                if(!$slug){
                    return $this->orderBy('posts.id''DESC')
                                ->findAll();  
                }
                return $this->asArray()
                            ->where(['slug' => $slug])
                            ->first();
                            
            }
        
        }

here the function getPosts will fetch all posts from database by posts id in descending order as an array format. also we are passing parameter slug(seo friendly url)

and now create a controller for our Blog Pages.. BlogController.php

<?php namespace App\Controllers;

use App\Models\BlogModel;
use CodeIgniter\Controller;

class Blog extends Controller
{
   
    public function index(){

      
        $session = session();
        helper(['text','url']);
       
         $model = new BlogModel();
         
        $data['blog_posts'] = $model->getPosts();
        $data['sort'] = $model->orderBy('posts.id''DESC');
       
        $data = [
            'blog_posts' =>$model->paginate(10),
            'pager' => $model->pager,
        ];
        $data['title'] = $data['blog_posts']['title'];
        $data['description'] = $data['blog_posts']['description'];
      
        echo view('blog/home',$data);
       
       
      }
   

This is our Blog's home page that is index page if user search our site then he/she will redirected to this page. 


Notice i have paginated this i.e at most 10 posts will be shown in our main page.



will update soon......



Leave a Comment

No comments:

Powered by Blogger.