আগামী ২৩ অক্টোবর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML,CSS,Bootstrap,PHP,MySQl, AJAX and JQUERY কোর্সের ৮৬ তম ব্যাচ। আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722817591, Email : [email protected] কোর্সের সিলেবাস এর জন্য এখানে ক্লিক করুন ।

Build a CMS using Codeigniter part-2

Hello guyz! We’re going to head through the second part of this tutorial. Here we’re going to learn some amazing tricks about controller & model. Now those who really missed the first part of this tutorial please do read & test the thing first. We’ll discuss something here that’ll also related to the other part. Why only this part every part will be related to other parts. So please do review the older parts before proceeding to the last part. Click Here you can find the other tutorials.

When building a fairly complex application it’s always a good idea to plan your code correctly. This is a tutorial that we’re going to learn about how to give a solid foundation while making these kind of complex application.

Setting Up Base Controller:

First off all go to the the controller folder let us start with admin panel, so for that create a folder named admin & in there create a controller page named “dashboard.php”. Now as we’ve created our just think once very carefully if we extend “CI_Controller” class every time we’ve to put the doing code in every controller. So to ignore that much coding we should create a base controller & extend a class to our base controller so that whatever we do, we do it in our base controller so that we don’t have to over write the old codings. Now go to core folder & in there create a file named “MY_Controller.php”. In there just paste the code given below:

class MY_Controller extends CI_Controller {

public $data = array();

function __construct() {

parent::__construct();

$this->data['errors'] = array();

$this->data['site_name'] = config_item('site_name');

}

}

In the upper code what we’ve done in here is that we just create a class called “MY_Controller” that extends “CI_Controller”, in there we’ve put a public property there which contains some array data & in the constructor method we’ve put some keys to the data array & that could be data[‘errors’] which is an empty array & then data[‘sitename’] which contains the site name that we’ve created in earlier post in the config file. Now as we’ve done in the core file let’s go to the welcome controller & see if this things work. Just change the code to the code given below:

class Welcome extends MY_Controller {

public function index(){

var_dump($this->data);

$this->load->view('welcome_message');

}

}

After changing the controller name & dumpling our data into it you may see the page like the below one:

mycontrollervardump

Now we may use two more controller one is a frontend controller & the second one is the admin controller. Why we’re making these two controller is we can use to frontend controller to show up the menu items & everything else in admin controller we may use this one for admin configuration. So to do that go to libraries folder & create two file named “Frontend_Controller.php” & “Admin_Controller.php”.

Now open the “Frontend_Controller.php” file & paste the code below:

class Frontend_Controller extends MY_Controller {

function __construct() {

parent::__construct();

var_dump('Welcome From Frontend Controller !! ');

}

}

In here we can see that we did anything only dumping some string, but keep it like that we’ll code here after some time in near future. So go to the welcome controller & then extend the welcome class to Frontend_Controller instead of MY_Controller. Let’s see what happens in there. But before reloading your page in the browser go the config file in config folder so that we can autoload the class.  For that paste the code below at the beginning of the file.

function __autoload($classname){

if (strpos($classname, 'CI_') !== 0) {

$file = APPPATH . 'libraries/' . $classname . '.php';

if (file_exists($file) && is_file($file)) {

@include_once($file);

}

}

}

Here what we did is that just autoloaded the classname & checks if the classname begins with the ‘CI_’ prefix & then setting the file path & again checking if the file exists then we’ll include the file with the class name. What’s the reason to do this thing is when we extend the welcome controller to frontend controller the codeigniter will never find the frontend contoller file so to include that file we’ve autoloaded this file via __autolad function. If no error occurs you should find a msg with like the picture below while browsing the project:

frontendinmycontroller

This is the end of the base controller part. Now we are going to use the database means using the Model. So let’s start using model.

Setting Up Data Model:

We’re going to use Migration in here. Those who don’t know anything about migration I’ll tell you to please fist visit the user guide for migration & you’ll get clear idea about migration in there. First off all open the migration.php file inside the application->config directory then you should need to edit the code in there. First just change $config[‘migration_enabled’] = FALSE;” this part of the code to this “$config[‘migration_enabled’] = TRUE;” & in here “$config[‘migration_version’] = 0;” in the place of 0 you’re going to put the version of migration that you want. For now it’ll be “1“. We’re going to learn the handling of migration in there just in a few minutes.

Now in the application directory create a folder named “migration” & in there crate a new file named 001_create_users.php”. Here in the beginning of the file name we put a number & this is the number of migration version that we’re gonna put in the migration config file. Now open the file & paste the below code:

<!--?php class Migration_Create_users extends CI_Migration { public function up(){ $this--->dbforge->add_field(array(

'id' => array(

'type' => 'INT',

'constraint' => 11,

'unsigned' => TRUE,

'auto_increment' => TRUE

),

'name' => array(

'type' => 'VARCHAR',

'constraint' => '100',

),

'email' => array(

'type' => 'VARCHAR',

'constraint' => '100',

),

'password' => array(

'type' => 'VARCHAR',

'constraint' => '128',

),

));

$this->dbforge->add_key('id',TRUE);

$this->dbforge->create_table('users');

}

public function down(){

$this->dbforge->drop_table('users');

}

}

Here we can see that we’ve created a class named ” Migration_Create_users” remember migration classes always needed the name migration at the beginning of it’s name. Now the Up function runs when we do a migration and down function runs when we do a rollback. Then what we’ve done in here we’re passing an array to the “add_field” method to add the field in the table. And in down function we’re using the “drop_table” method to drop the users table. The most interesting part in here is that we’ve set all the things using PHP & that’s the good thing about migration we don’t have to write codes in database from now on.

Now to run the migration go to the “controller->admin” directory & create a controller file named “migration.php” and then paste the code from below in the there:

class Migration extends Admin_Controller {

function __construct() {

parent::__construct();

}

public function index(){

//Loading the library of migration using this library() method

$this->load->library('migration');

//If the current migration status is in

//the file than show a successful massage

//else echo an error

if ($this->migration->current()){
echo "Migration Worked!";
}
else{
show_error($this->migration->error_string());
}

}

}

In the upper code just see the comment above the code. I think you guys just understood the running method of migration. Now just go to the browse using this url http://localhost/ci//index.php/admin/migration” & see in php myadmin if everythings fine if there’s a table created or not. Look at the picture below to be sure how it looks when the migration works look at the url also.

migrationrunning

Now let’s create another file in migration folder & name it ” 002_create_pages.php ” & paste the code from below:

class Migration_Create_pages extends CI_Migration {

public function up()

{

$this->dbforge->add_field(array(

'id' => array(

'type' => 'INT',

'constraint' => 11,

'unsigned' => TRUE,

'auto_increment' => TRUE

),

'title' => array(

'type' => 'VARCHAR',

'constraint' => '100',

),

'slug' => array(

'type' => 'VARCHAR',

'constraint' => '100',

),

'order' => array(

'type' => 'INT',

'constraint' => '11',

),

'body' => array(

'type' => 'TEXT',

),

));

$this->dbforge->add_key('id',TRUE);

$this->dbforge->create_table('pages');

}

public function down()

{

$this->dbforge->drop_table('pages');

}

}

It’s easier now to understand what i coded in the upper section. Now change the migration version from migration file in config directory & run the migration again  & see if the table created correctly in db. This is the part of our data model. We’re going to use these data model in forward.  For surety please check if the database just look like the picture below:

datamodal

We’ve done so many things creating our base controllers, using migration to set our data model. This is today’s tutorial. We’re gonna be back with more parts of this tutorial so just hangout with us & stay tuned for the next part. Hope you’d liked the article & it’s helpful to you. Please don’t forget to share your thoughts.

To download the full source code please hit the download button.

An enthusiastic of work with over 2+ years of experience. Extensively experienced on wordpress, Codeigniter, Laravel. ##Visit my github page (https://github.com/ashawkat) ##Visit my blogging page where I spread out my knowledges around all other people (http://w3programmers.com/author/tanim) If you want to hire me then you can contact me via email or skype. Skype: ashawkat89 Email: [email protected]
14 comments on “Build a CMS using Codeigniter part-2
  1. I just like the helpful information you supply for your articles.

    I will bookmark your weblog and take a look at once more here frequently.
    I am fairly certain I’ll be informed lots of new stuff right right here! Best of luck for the following!

  2. Hi everyone, it’s my first go to see at this web page, and paragraph is actually fruitful in support of me, keep up posting these content.

    • it’s a cms site bro ! i was unable to post the other parts because of work load but will post those shortly i think ! & yes of course wp is the best choice for blog & i’m trying to build up here a cms site ! you’ll need these sort of things for sure as you can’t build any app with wordpress ! 🙂

  3. when i put this code:
    class Welcome extends MY_Controller {

    public function index(){

    var_dump($this->data);

    $this->load->view(‘welcome_message’);

    }

    }
    then run this but the display screen have nothing(full white).plz say whats the problem.

  4. Can I just say what a relief to find a person that truly knows what they’re discussing on the net. You certainly know how to bring an issue to light and make it important. A lot more people have to look at this and understand this side of your story. I was surprised you’re not more popular because
    you most certainly possess the gift.

Leave a Reply

Your email address will not be published. Required fields are marked *