Loading custom config file into a Codeigniter library - codeigniter

I know this is probably simple, but I'm not getting. I've created a library, and I want to load the parameters from a config file. So here's an example of what I have:
// libraries/Mylib.php
class Mylib {
var $ci;
var $key;
public function _construct {
$this->ci =& get_instance();
$this->ci->config->load('mylib');
$this->key = $this->ci->config->item('key');
}
public function myKey() {
return "Key=" . $this->key;
}
}
// config/mylib.php
$config['key'] = 'randomcharacters';
I load the library, and try to print out the myKey function, but it just returns "Key=", without the actual key. What am I missing?

It seems like you missed an underscore for your constructor:
instead of
public function _construct () {
you should use
public function __construct () {

Related

Pass variables to multiple view in laravel

I want to pass a variable to multiple view bu when i use share method in View. Its says the share method isn't find on View.
How you say i use it and i try the composer either but no matter how i try it can't work could you give me simple example of this action
My controller categorycontroller.php
public function site(){
$data = array();
$data['subcategories'] = SubCategory::all();
$data['categories'] = Category::all();
return view('template.sitemap',compact("data"));
}
My route web.php
Route::get('/404.html', function () {
return view('template/404');
})->name('404.html');
Route::get('404.html','CategoryController#site')->name('404');
Route::get('/sitemap.html', function () {
return view('template/sitemap');
})->name('sitemap.html');
Route::get('sitemap.html','CategoryController#site')->name('sitemap');
what do you suggest?
You can make a variable accessible in multiple views using one of these methods for example:
AppServiceProvider ( reference: https://laravel.com/docs/5.6/providers ) with ViewComposer ( reference: https://laravel.com/docs/master/views#view-composers )
You'll need to add to your ServiceProvider boot() method something similar to this:
public function boot()
{
View::share('variable_name', 'some_value_here');
}
Or inside a controller:
public function __construct() {
$something = 'just a test';
View::share('something', $something);
}

helper is not loading in Codeigniter model

<?php
class MyModel extends CI_Model {
public function loadData()
{
$CI =& get_instance();
$CI->load->helper('data_helper');
print_r($CI->data_helper); //this is printing nothing
$CI->data_helper->loaditems(); // method is not calling
}
}
function loaditems()
{
echo "hello from load of helper";
}
?>
helper filename is data_helper.php
give me you thought about this why it not working and in which case it will work
Put the file data_helper.php in the /application/helpers directory.
In /application/config/autoload.php load the helper using just the word 'data'. (line 92).
$autoload['helper'] = array('data');
Or you can load it before you need it with $this->load->helper('data');
Then you can use loaditems() from anywhere like a normal function.
You don't need the $CI magic at all.
According to the documentation
$this->load->helper('name');
Where name is the file name of the helper, without the .php file extension or the “helper” part.
which means the following code should work
class MyModel extends CI_Model
{
public function loadData()
{
$this->load->helper('data');
loaditems();
}
}
you can read more about it here
Try data_helper() to call helper function.
data_helper();

laravel model event static call issue

Why my static method don´t work with a varible class?
/**
* Events
*/
public static function boot() {
parent::boot();
$class = get_called_class(); // The value os $class is: Product ( string)
// This work
Product::creating(function($model) {
return $model->validate();
});
// Don´t work, the closure never called!
$class::creating(function($model) {
return $model->validate();
});
$class::updating(function($model) {
return $model->validate(true);
});
}
Incredible, this work to:
$class = "Product"; //get_called_class();
Solution ( Not elegant , but ... )
On my Product model i put this, to share class name with Base model.
public static function boot() {
parent::$cls = "Product";
parent::boot();
}
but updating does not work yet!
Actually (since PHP 5.3) it should work to do:
$class = 'Product';
$class::creating(function($model){
return $model->validate();
});
But why not just use static:: to access the current class? This way you don't need get_called_class:
static::creating(function($model){
return $model->validate();
});
There's also call_user_func if everything else fails...

Accessing global variable accross methods confusion in Codeigniter?

This is what I have:
class Calendar extends CI_Controller {
public $extension;
function __construct()
{
parent::__construct();
$this->extension = "";
}
public function index($page_id, $extension=null)
{
if(!is_null($extension))
{
$this->extension = $extension;
}
$this->firephp->log($this->extension);
$this->load->view('/modules/calendar_view', array("page_id" => $page_id, "extension" => $this->extension));
}
public function update_calendar($width, $page_id, $new_month=null, $new_year=null)
{
$this->firephp->log($this->extension);
}
}
The correct value echos out in the index method fine, obviously. But the view it loads then calls the second method and the global variable is not set.
Is there a way of setting it until it's explicitly changed?
Thanks.
public $extension is not a global variable here. I believe flashdata is what you are looking for
$this->session->set_flashdata('item', 'value');
In your next call you can get the item as
$this->session->flashdata('item');
These are flushed in next execution.

Convenient Way to Load Multiple Databases in Code Igniter

I've added the appropriate configuration arrays to database.php and they work, however, I would like an easier way to access the different databases. Right now I have to do something like this in every controller method:
function index(){
$BILLING = $this->load->database('billing', TRUE);
$INVENTORY = $this->load->database('inventory', TRUE);
$data['billing'] = $BILLING->get('stuff');
$data['inventory'] = $INVENTORY->get('stuff');
}
I'd like to be able to put those first two lines in some sort of before filter or pre_controller hook.
You could simply load the database instances globally in your constructor, then they would be available to all controller methods...
example controller
class Example extends CI_Controller {
//declare them globally in your controller
private $billing_db;
private $inventory_db;
function __construct() {
parent::__construct();
//Load them in the constructor
$this->billing_db = $this->load->database('billing', TRUE);
$this->inventory_db = $this->load->database('inventory', TRUE);
}
function index() {
//Then use them in any controller like this
$data['billing'] = $this->inventory_db->get('stuff');
$data['inventory'] = $this->billing_db->get('stuff');
}
}
And if these same databases are used across multiple controllers, you might consider extending the base controller to include these global variables and load them in the constructor of your base controller in MY_Controller.php
example MY_Controller.php
class DB_Controller extends CI_Controller {
//declare them globally in your controller
private $billing_db;
private $inventory_db;
function __construct() {
parent::__construct();
//Load them in the constructor
$this->billing_db = $this->load->database('billing', TRUE);
$this->inventory_db = $this->load->database('inventory', TRUE);
}
}
Then you'd use it like this...
class Example extends DB_Controller {
function __construct() {
parent::__construct();
}
function index() {
//Then use them in any controller like this
$data['billing'] = $this->inventory_db->get('stuff');
$data['inventory'] = $this->billing_db->get('stuff');
}
}

Resources