Welcome Guest, Not a member yet? Register   Sign In
Codeigniter 3 blog application: overwriting default “static” variable with a “dynamic
#1

I am working on a blog application in Codeigniter 3.1.8.

I have a model with "static" data like the website's title, the contact email address, etc:



Code:
class Static_model extends CI_Model {
   public function get_static_data() {
       $data['site_title'] = "My Blog";
       $data['tagline'] = "A simple blog application made with Codeigniter 3";
       $data['company_name'] = "My Company";
       $data['company_email'] = "[email protected]";
       return $data;
   }
}

In my Posts Controller, I have tried to flow the DRY principle this way:

Code:
class Posts extends CI_Controller {

   public function __construct()
   {
     parent::__construct();
     // Load static data
     $this->load->model('Static_model');
     $data = $this->Static_model->get_static_data();
     // Load Header
     $this->load->view('partials/header', $data);
   }

   public function index()
   {
       $this->load->model('Posts_model');
       $data['posts'] = $this->Posts_model->get_posts();
       $this->load->view('posts', $data);
       $this->load->view('partials/footer');
   }

   public function post($id)
   {
       $this->load->model('Posts_model');
       $data['post'] = $this->Posts_model->get_post($id);

       if (!empty($data['post'])) {
           // Overwrite the default tagline with the post title
           $data['tagline'] = $data['post']->title;
       } else {
           $data['tagline'] = "Page not found";
           show_404();
       }

       $this->load->view('post', $data);
       $this->load->view('partials/footer');
   }

}

See details of how I came about writing the code above in this topic on stackoverflow.com


The problem with the above code is that the line 

Code:
$data['tagline'] = $data['post']->title;

 does no longer overwrite the static tagline 

Code:
$data['tagline'] = "A simple blog application made with Codeigniter 3";

 with the post title. It did overwrite it when the controller look like this:
class Posts extends CI_Controller {

Code:
   public function index()
   {
       $this->load->model('Static_model');
       $data = $this->Static_model->get_static_data();

       $this->load->model('Posts_model');
       $data['posts'] = $this->Posts_model->get_posts();

       $this->load->view('partials/header', $data);
       $this->load->view('posts');
       $this->load->view('partials/footer');
   }

   public function post($id) {
       $this->load->model('Static_model');
       $data = $this->Static_model->get_static_data();

       $this->load->model('Posts_model');
       $data['post'] = $this->Posts_model->get_post($id);

       // Overwrite the default tagline with the post title
       $data['tagline'] = $data['post']->title;

       $this->load->view('partials/header', $data);
       $this->load->view('post');
       $this->load->view('partials/footer');
   }

}

Bot this old version of it is goes against the DRY principle.



How can I do the desired overwrite without breaking the "DRY commandment"?
Reply
#2

Maybe this will help you get an idea on how to do it.

./application/libraries/Registry.php
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * -----------------------------------------------------------------------
 * Editor   : PhpStorm 2018.1
 * Date     : 01/23/2018
 * Time     : 6:03 AM
 * Authors  : Raymond L King Sr.
 * -----------------------------------------------------------------------
 * 
 * Class        Name
 * 
 * @project     starter
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2018 Pro Covers FX, LLC.
 * @license     http://www.procoversfx.com/license
 * -----------------------------------------------------------------------
 */

// -----------------------------------------------------------------------

/**
 * SETUP:
 *
 * place this library in ./application/libraries/Registry.php
 *
 * Autoload: ./application/libraries/Registry.php
 * Autoload: ./application/helpers/registry_helper.php
 *
 * TESTING:
 *
 * In your Controller index method add.
 *
 *         regSet('1', 'test1', 'test1');
 *         regSet('1', 'test2', 'test1');
 *         regSet('1', 'test3', 'test1');
 *
 *         regSet('2', 'test4', 'test2');
 *         regSet('2', 'test5', 'test2');
 *         regSet('2', 'test6', 'test2');
 *
 *         $result  = regGet('1', 'test3');
 *         $result1 = regGet_index('2');
 *         $exist   = regExists('2', 'test1');
 *
 *         $result2 = regDelete('1', 'test3');
 *
 *         // Remove the remarks // to reset and clear out the reg array.
 *         // regClear();
 *
 * DEBUG:
 *
 *         var_dump($this->registry->debugRegistry());
 *
 *         $this->registry->var_debug($this->registry->debugRegistry(), $result, $result1, $exist, $result2);
 *
 * USAGE:
 *
 * set:            $this->registry->set('1', 'test1', 'test1');
 * get:            $result = $this->registry->get('1', 'test3');
 * get_index    $result = $this->registry->get_index('2');
 * exists:        $result = $this->registry->exists('2', 'test1');
 * clear:        $this->registry->clear();
 * delete:        $result2 = $this->registry->delete('1', 'test3');
 *
 */

class Registry {

    
/**
     * Class variables - public, private, protected and static.
     * -------------------------------------------------------------------
     */

    /**
     * @var
     * CI Super Object.
     */
    
private $_ci;

    
/**
     * @var
     * array ( $index => array ( $key => $val ));
     */
    
protected static $reg = array(array());

    
// -------------------------------------------------------------------

    /**
     * __construct ()
     * -------------------------------------------------------------------
     *
     * Constructor    PHP 5+
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    
public function __construct()
    {
        
$this->_ci =& get_instance();

        
log_message('debug'"Registry Class Initialized");
    }

    
// -------------------------------------------------------------------

    /**
     * set ()
     * -------------------------------------------------------------------
     *
     * Set a registry index and key, value pair.
     *
     * @param   $index
     * @param   $key
     * @param   $val
     */
    
public function set($index$key$val)
    {
        if ( ! isset(
self::$reg[$index][$key]))
        {
            
self::$reg[$index][$key] = $val;
        }
    }

    
// -------------------------------------------------------------------

    /**
     * get ()
     * -------------------------------------------------------------------
     *
     * Gets a registry and value.
     *
     * @param   $index
     * @param   $key
     * @return  bool
     */
    
public function get($index$key)
    {
        if (isset(
self::$reg[$index][$key]))
        {
            return 
self::$reg[$index][$key];
        }

        return 
false;
    }

    
// -------------------------------------------------------------------

    /**
     * getIndex ()
     * -------------------------------------------------------------------
     *
     * Gets the array for this index.
     *
     * @param   null $index
     * @return  bool|mixed
     */
    
public function getIndex($index null)
    {
        if (
$index != null)
        {
            return 
self::$reg[$index];
        }

        return 
false;
    }

    
// -------------------------------------------------------------------

    /**
     * exists ()
     * -------------------------------------------------------------------
     *
     * Checks to see if a registry exists.
     *
     * @param   $index
     * @param   $key
     * @return  bool
     */
    
public function exists($index$key)
    {
        return isset(
self::$reg[$index][$key]);
    }

    
// -------------------------------------------------------------------

    /**
     * clear ()
     * -------------------------------------------------------------------
     *
     * Clears out and resets the registry arrays.
     */
    
public function clear()
    {
        
self::$reg = array(array());
    }

    
// -------------------------------------------------------------------

    /**
     * delete ()
     * -------------------------------------------------------------------
     *
     * Deletes a registry index key.
     *
     * @param   $index
     * @param   $key
     * @return  bool
     */
    
public function delete($index$key)
    {
        if (isset(
self::$reg[$index][$key]))
        {
            unset(
self::$reg[$index][$key]);

            return 
true;
        }

        return 
false;
    }

    
// -------------------------------------------------------------------

    /**
     * debugRegistry ()
     * -------------------------------------------------------------------
     *
     * Debug the reg arrays.
     *
     * @access  public
     * @return  array
     */
    
public function debugRegistry()
    {
        return 
self::$reg;
    }

  // End of Registry Class.

/**
 * -----------------------------------------------------------------------
 * Filename: Registry.php
 * Location: ./application/libraries/Registry.php
 * -----------------------------------------------------------------------
 */ 

./application/helpers/registry_helper.php
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * -----------------------------------------------------------------------
 * Editor   : PhpStorm 2018.1
 * Date     : 01/23/2018
 * Time     : 6:27 AM
 * Authors  : Raymond L King Sr.
 * -----------------------------------------------------------------------
 * 
 * Class        registry_helper
 * 
 * @project     starter
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2018 Pro Covers FX, LLC.
 * @license     http://www.procoversfx.com/license
 * -----------------------------------------------------------------------
 */

// -----------------------------------------------------------------------

/**
 * regSet ()
 *
 * Sets a new property.
 *
 * Usage: regSet($index, $key, $val);
 */
if ( ! function_exists('regSet'))
{
    
/**
     * regSet ()
     * -------------------------------------------------------------------
     *
     * @param   string $index
     * @param   string $key
     * @param   string $val
     */
    
function regSet($index ''$key ''$val '')
    {
        
ci()->registry->set($index$key$val);
    }
}

// -----------------------------------------------------------------------

/**
 * regGet ()
 *
 * Gets a registry value.
 *
 * Usage: $result = regGet($index, $key);
 */
if ( ! function_exists('regGet'))
{
    
/**
     * regGet ()
     * -------------------------------------------------------------------
     *
     * @param   string $index
     * @param   string $key
     * @return  mixed
     */
    
function regGet($index ''$key '')
    {
        
$_ci =& get_instance();
        return 
$_ci->registry->get($index$key);
    }
}

// -----------------------------------------------------------------------

/**
 * regGetIndex ()
 *
 * Get a registry index.
 *
 * Usage: $result = regGetIndex($index);
 */
if ( ! function_exists('regGetIndex'))
{
    
/**
     * regGetIndex ()
     * -------------------------------------------------------------------
     *
     * @param   null $index
     * @return  mixed
     */
    
function regGetIndex($index null)
    {
        
$_ci =& get_instance();
        return 
$_ci->registry->getIndex($index);
    }
}

// -----------------------------------------------------------------------

/**
 * regExists ()
 *
 * Checks to see if a registry index and key exists.
 *
 * Usage: $result = regExists($index, $key);
 */
if ( ! function_exists('regExists'))
{
    
/**
     * regExists ()
     * -------------------------------------------------------------------
     *
     * @param   string $index
     * @param   string $key
     * @return  mixed
     */
    
function regExists($index ''$key '')
    {
        
$_ci =& get_instance();
        return 
$_ci->registry->exists($index$key);
    }
}

// -----------------------------------------------------------------------

/**
 * regDelete ()
 *
 * Deletes a registry index and key.
 *
 * Usage: $result = regDelete($index, $key);
 */
if ( ! function_exists('regDelete'))
{
    
/**
     * regDelete ()
     * -------------------------------------------------------------------
     *
     * @param   string $index
     * @param   string $key
     * @return  mixed
     */
    
function regDelete($index ''$key '')
    {
        
$_ci =& get_instance();
        return 
$_ci->registry->delete($index$key);
    }
}

// -----------------------------------------------------------------------

/**
 * regClear ()
 *
 * Resets and clears out the registry arrays.
 *
 * Usage: regClear();
 */
if ( ! function_exists('regClear'))
{
    
/**
     * reg_clear ()
     * -------------------------------------------------------------------
     *
     */
    
function regClear()
    {
        
$_ci =& get_instance();
        
$_ci->registry->clear();
    }
}

/**
 * -----------------------------------------------------------------------
 * Filename: registry_helper.php
 * Location: ./application/helpers/registry_helper.php
 * -----------------------------------------------------------------------
 */ 

That's just one way of doing it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB