[eluser]InsiteFX[/eluser]
Actually I have written a Registry Library for CI
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* ------------------------------------------------------------------------
* Created by Php Designer 8.
* Date : 10/10/2013
* Time : 4:02:10 AM
* The Learn CodeIgniter Development Team.
* ------------------------------------------------------------------------
*
* Class Registry
*
* @package Package Application
* @subpackage Subpackage properties
* @category category properties
* @author Raymond L King Sr.
* @copyright Copyright (c) 2009 - 2014, Custom Software Designers, LLC.
* @link http://example.com
* ------------------------------------------------------------------------
* To change this template use File | Settings | File Templates.
* ------------------------------------------------------------------------
*/
/**
* 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.
*
* reg_set('1', 'test1', 'test1');
* reg_set('1', 'test2', 'test1');
* reg_set('1', 'test3', 'test1');
*
* reg_set('2', 'test4', 'test2');
* reg_set('2', 'test5', 'test2');
* reg_set('2', 'test6', 'test2');
*
* $result = reg_get('1', 'test3');
* $result1 = reg_get_index('2');
* $exist = reg_exists('2', 'test1');
*
* $result2 = reg_delete('1', 'test3');
*
* // Remove the remarks // to reset and clear out the reg array.
* // reg_clear();
*
* DEBUGING:
*
* var_dump($this->registry->debug_properties());
*
* $this->registry->var_debug($this->registry->debug_registry(), $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.
* -----------------------------------------------------------------------
*/
/**
* array ( $index => array ( $key => $val ));
*/
private static $reg = array(array());
// -----------------------------------------------------------------------
/**
* __construct()
*
* Constructor PHP 5+ NOTE: Not needed if not setting values!
*
* @access public
* @return void
*/
public function __construct()
{
log_message('debug', 'Registry Class Initialized');
}
Continued below
[eluser]InsiteFX[/eluser]
Code: // -----------------------------------------------------------------------
/**
* set()
*
* Set a registry index and key, value pair.
*
* @access public
* @param string - $index - The registry index
* @param string - $key - The registry key
* @param string - $val - The registry value
* @return void
*/
public function set($index, $key, $val)
{
if ( ! isset($this->reg[$index][$key]))
{
$this->reg[$index][$key] = $val;
}
}
// -----------------------------------------------------------------------
/**
* get()
*
* Gets a registry and value.
*
* @access public
* @param string - $index - The registry index
* @param string - $key - The registry key
* @return mixed - registry string or boolean FALSE
*/
public function get($index, $key)
{
if (isset($this->reg[$index][$key]))
{
return $this->reg[$index][$key];
}
return FALSE;
}
// -----------------------------------------------------------------------
/**
* get_index()
*
* Gets the array for this index.
*
* @access public
* @param string - $index
* @return mixed
*/
public function get_index($index = NULL)
{
if ($index != NULL)
{
return $this->reg[$index];
}
return FALSE;
}
// -----------------------------------------------------------------------
/**
* exists()
*
* Checks to see if a registry exists.
*
* @access public
* @param string - $index - The registry index
* @param string - $key - The registry key
* @return bool - TRUE if the registry exists otherwise FALSE.
*/
public function exists($index, $key)
{
return isset($this->reg[$index][$key]);
}
// -----------------------------------------------------------------------
/**
* clear()
*
* Clears out and resets the registry arrays.
*
* @access public
* @return void
*/
public function clear()
{
$this->reg = array(array());
}
// -----------------------------------------------------------------------
/**
* delete()
*
* Deletes a registry index key.
*
* @access public
* @param string - $index - The registry index
* @param string - $key - The registry key
* @return bool
*/
public function delete($index, $key)
{
if (isset($this->reg[$index][$key]))
{
unset($this->reg[$index][$key]);
return TRUE;
}
return FALSE;
}
// -----------------------------------------------------------------------
/**
* debug_registry()
*
* Debug the reg arrays.
*
* @access public
* @return array
*/
public function debug_registry()
{
return $this->reg;
}
} // End of Class
/**
* -------------------------------------------------------------------------
* Filename: Registry.php
* Location: ./application/libraries/Registry.php
* -------------------------------------------------------------------------
*/
Continued below
[eluser]InsiteFX[/eluser]
Registry_helper
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* ------------------------------------------------------------------------
* Created by Php Designer 8.
* Date : 10/20/2013
* Time : 11:29:08 AM
* Author: Raymond L King Sr.
* The Learn CodeIgniter Development Team.
*
* Helper registry_helper
*
* ------------------------------------------------------------------------
* To change this template use File | Settings | File Templates.
* ------------------------------------------------------------------------
*/
// ------------------------------------------------------------------------
/**
* reg_set()
*
* Sets a new property.
*
* Usage: reg_set($index, $key, $val);
*
* @access public
* @param array
* @return mixed depends on what the array contains
*/
if ( ! function_exists('reg_set'))
{
function reg_set($index = '', $key = '', $val = '')
{
$_ci = get_instance();
$_ci->registry->set($index, $key, $val);
}
}
// ------------------------------------------------------------------------
/**
* reg_get()
*
* Gets a registry value.
*
* Usage: $result = reg_get($index, $key);
*
* @access public
* @param array
* @return mixed depends on what the array contains
*/
if ( ! function_exists('reg_get'))
{
function reg_get($index = '', $key = '')
{
$_ci = get_instance();
return $_ci->registry->get($index, $key);
}
}
// ------------------------------------------------------------------------
/**
* reg_get_index()
*
* Get a registry index.
*
* Usage: $result = reg_get_index($index);
*
* @access public
* @param array
* @return mixed depends on what the array contains
*/
if ( ! function_exists('reg_get_index'))
{
function reg_get_index($index = NULL)
{
$_ci = get_instance();
return $_ci->registry->get_index($index);
}
}
// ------------------------------------------------------------------------
/**
* reg_exists()
*
* Checks to see if a registry index and key exists.
*
* Usage: $result = reg_exists($index, $key);
*
* @access public
* @param array
* @return mixed depends on what the array contains
*/
if ( ! function_exists('reg_exists'))
{
function reg_exists($index = '', $key = '')
{
$_ci = get_instance();
return $_ci->registry->exists($index, $key);
}
}
// ------------------------------------------------------------------------
/**
* reg_delete()
*
* Deletes a registry index and key.
*
* Usage: $reult = reg_delete($index, $key);
*
* @access public
* @param array
* @return mixed depends on what the array contains
*/
if ( ! function_exists('reg_delete'))
{
function reg_delete($index = '', $key = '')
{
$_ci = get_instance();
return $_ci->registry->delete($index, $key);
}
}
// ------------------------------------------------------------------------
/**
* reg_clear()
*
* Resets and clears out the registry arrays.
*
* Usage: reg_clear();
*
* @access public
* @param array
* @return mixed depends on what the array contains
*/
if ( ! function_exists('reg_clear'))
{
function reg_clear()
{
$_ci = get_instance();
$_ci->registry->clear();
}
}
/**
* ------------------------------------------------------------------------
* Filename: registry_helper.php
* Location: ./application/helpers/registry_helper.php
* ------------------------------------------------------------------------
*/
Enjoy
[eluser]ivantcholakov[/eluser]
@ InsiteFX
Yes, I have mine too.
Code: <?php defined('BASEPATH') OR exit('No direct script access allowed.');
/**
* Registry library for CodeIgniter
*
* You may use this library for storing and accessing application-level global data.
*
* Installation:
*
* Put this file Registry.php within application/libraries/ folder of your project.
*
* Usage Example:
*
* //---------------------------------------------------------------------------
* // Context #1:
*
* $ci = get_instance(); // Use $this instead of $ci inside a controller's method.
* $ci->load->library('registry'); // You may autoload this library at will.
*
* $title = 'Page Title';
* $subtitle = 'Page Subtitle';
* $metatitle = 'Page Title (Meta)';
* $metadescription = 'Page Description (Meta)';
* $metakeywords = 'page, keywords, meta';
*
* $ci->registry
* // Method chaining is possible.
* // Set values individually:
* ->set('page_title', $title)
* ->set('page_subtitle', $subtitle)
* // Set multiple values.
* ->set(compact('metatitle', 'metadescription', 'metakeywords'))
* ;
*
* unset($title, $subtitle, $metatitle, $metadescription, $metakeywords);
*
* //---------------------------------------------------------------------------
* // Context #2:
*
* $ci = get_instance();
* $ci->load->library('registry');
*
* // Get values individually.
* $title = $ci->registry->get('page_title');
* $subtitle = $ci->registry->get('page_subtitle');
*
* // Get multiple values.
* extract($ci->registry->get(array('metatitle', 'metadescription', 'metakeywords')));
*
* // Test:
* var_dump(compact('title', 'subtitle', 'metatitle', 'metadescription', 'metakeywords'));
*
* //---------------------------------------------------------------------------
* // Also:
*
* // Check whether a particular value is present.
* $test = $ci->registry->has('test_key');
* var_dump($test);
*
* // Gets everything from the registry (for debugging purpose).
* $registry = $ci->registry->get_all();
* var_dump($registry);
*
* // Unset values.
* $ci->registry
* ->delete('page_title')
* ->delete('page_subtitle')
* ->delete(array('metatitle', 'metadescription', 'metakeywords'))
* ;
* var_dump($ci->registry->get_all());
*
* // Use destroy method only for testing purposes
* $ci->registry->destroy();
* var_dump($ci->registry->get_all());
*
* //---------------------------------------------------------------------------
*
* @author Ivan Tcholakov <[email protected]>, 2014
* @license The MIT License, http://opensource.org/licenses/MIT
*
* Code repository: https://github.com/ivantcholakov/codeigniter-registry
*/
class Registry {
protected static $data = array();
public function __construct() {
log_message('debug', 'Registry class initialized');
}
public function get($key) {
if (is_array($key)) {
$result = array();
foreach ($key as $k) {
$result[$k] = $this->get($k);
}
return $result;
}
$key = (string) $key;
if ($key != '' && array_key_exists($key, self::$data)) {
return self::$data[$key];
}
return null;
}
public function get_all() {
return self::$data;
}
public function set($key, $value = null) {
if (is_array($key)) {
foreach ($key as $k => $v) {
$this->set($k, $v);
}
return $this;
}
self::$data[(string) $key] = $value;
return $this;
}
public function has($key) {
$key = (string) $key;
if ($key != '' && array_key_exists($key, self::$data)) {
return true;
}
return false;
}
public function delete($key) {
if (is_array($key)) {
foreach ($key as $k) {
$this->delete($k);
}
return $this;
}
$key = (string) $key;
if ($key != '' && array_key_exists($key, self::$data)) {
unset(self::$data[$key]);
}
return $this;
}
// Use this method for testing purposes only!
public function destroy() {
self::$data = array();
return $this;
}
}
[eluser]ivantcholakov[/eluser]
... and the helper, it is very simple, I think just a getter is enough for so far.
Code: <?php defined('BASEPATH') OR exit('No direct script access allowed.');
/**
* @author Ivan Tcholakov <[email protected]>, 2014
* @license The MIT License, http://opensource.org/licenses/MIT
*/
if (!function_exists('registry')) {
function registry($key) {
$ci = get_instance();
$ci->load->library('registry');
return $ci->registry->get($key);
}
}
@ behnampmdg3
If you use HMVC, the Registry pattern would be handy. Just choose the names of the indexes carefully, the Registry is like a storage of global variables (which is isolated from PHP globals).
[eluser]InsiteFX[/eluser]
Right, I use HMVC all the time. It is the default in all my setups for CI.
Right now I'am not sure what's going to be happening with CI so I started using FuelPHP also months ago.
[eluser]behnampmdg3[/eluser]
[quote author="InsiteFX" date="1389447217"]Right now I'am not sure what's going to be happening with CI so I started using FuelPHP also months ago.
[/quote]I got this email from Ellislab Quote:Ben,
CodeIgniter 3.0 is still being worked on by the community, but there have been some snags along the way. Hopefully it'll be out soon, but I can't say when that will be.
Best Regards,
[eluser]behnampmdg3[/eluser]
Also I have no ideas to get the page data!
Please guide ;p
[eluser]behnampmdg3[/eluser]
[quote author="ivantcholakov" date="1389446952"]@ behnampmdg3
If you use HMVC, the Registry pattern would be handy. Just choose the names of the indexes carefully, the Registry is like a storage of global variables (which is isolated from PHP globals).[/quote]This is the first time I am in need of such thing. I need a way now to get the data that has been set in MY_Controller
[eluser]ivantcholakov[/eluser]
The idea is not to set data within the controller, but within the registry. And by using the registry library you can access data from everywhere.
An amendment: Store within registry only data that you use repetitively in different contexts.
|