Skip to main content

Creating a Module

Home Forums fuelCMS Creating a Module

Tagged: ,

Viewing 0 reply threads
  • Author
    Posts
    • #2207

      Layer7web
      Keymaster

      *** All of the code snippets taken from http://www.getfuelcms.com/. 🙂

      $config['modules']['guestbook'] = array(
           'model_name' => 'guestbookcomments_model',
           'model_location' => 'guestbook'
      );

      Make sure you have you database tables set up. Here is an example:

      CREATE TABLE IF NOT EXISTS guestbook_comments (
          id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
          author VARCHAR(25) NULL NULL,
          comment VARCHAR(100) NOT NULL,
          'date' DATETIME NOT NULL,
          visible ENUM('yes', 'no') NOT NULL DEFAULT 'yes',
      
          PRIMARY KEY (id)
      );

      Create the Model Class:

      <?php
      if (!defined('BASEPATH')) exit('No direct script access allowed');
      require_once(FUEL_PATH . 'models/base_module_model.php');
      
      class GuestbookComments_model extends Base_module_model
      {
          public function __construct()
          {
              parent::__construct('guestbook_comments');
          }
      }
      
      class GuestbookComment_model extends Base_module_record
      {
      }
      

      Create the Controller:

      <?php
      class Guestbook extends CI_Controller
      {
          function view()
          {
              echo '<h1>Guestbook</h1>';
              $this->load->module_model('guestbook', 'GuestbookComments_model');
              $comments = $this->GuestbookComments_model->find_all();
              foreach ($comments as $comment) {
                  echo '<h2>' . $comment->author . '</h2>';
                  echo $comment->comment;
              }
          }
      }

      Add Method to the Controller:

      <?php
      public function add()
      {
          $this->load->library(
              'form_builder',
              array(
                  'id'=>'addComment',
                  'form_attrs' => array(
                      'method' => 'post',
                      'action' => 'newcomment'
                  ),
                  'submit_value' => 'Add new comment',
                  'textarea_rows' => '5',
                  'textarea_cols' => '28'
              )
          );
      
          $fields = array(
              'name' => array(
                  'label' => 'Full Name',
                  'required' => true
              ),
              'comment' => array(
                  'type' => 'text',
                  'label' => 'Comment',
                  'required' => true
              )
          );
          $this->form_builder->set_fields($fields);
          echo $this->form_builder->render();
      }

      Create method in the Controller to handle the form submission:

      <?php
      public function newcomment()
      {
          $this->load->module_model('guestbook', 'GuestbookComments_model');
          $comment = $this->GuestbookComments_model->create();
          $comment->author = $this->input->post('name');
          $comment->date = date('Y-m-d G:i:s');
          $comment->comment = $this->input->post('comment');
          $comment->save();
      
          echo 'comment successfully added';
      }
      • This topic was modified 10 years, 3 months ago by Layer7web.
      • This topic was modified 10 years, 3 months ago by Layer7web.
      • This topic was modified 10 years, 3 months ago by Layer7web.
Viewing 0 reply threads

You must be logged in to reply to this topic.