Loading CodeIgniter custom libraries - codeigniter

I added a simple library/class to my application called Assets. The file is located at:
application/libraries/Assets.php
The content is:
class Assets {
public function get_images() {
....
}
}
In my controller I have:
function __construct() {
parent::__construct();
$this->load->library('assets');
}
And I call it using:
public function members() {
$data = array();
$data['images'] = $this->assets->get_images();
}
But I am getting this error:
An uncaught Exception was encountered
Type: Error
Message: Class 'CI_Assets' not found
Filename: ../system/core/Common.php
Line Number: 196
What could be causing this?

Try change class name
CI_Assets

Try this
$this->load->library('Assets');
$assets = new Assets();
$assets->get_images();

Related

How to fix this error: Call to a member function get() on a non-object?

Fatal error: Call to a member function get() on a non-object in
C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Paging\application\models\name_model.php
Severity: Error
Message: Call to a member function get() on a non-object
Filename: models/name_model.php
Line Number: 13
Backtrace:
models/name_model.php:
class Name_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_name()
{
$query = $this->db->get('name');
return $query->result();
}
}
Line 13: $query = $this->db->get('name');
How to fix the error message?
You didn't load database libraries that why you getting this error.
Open your autoload.php file and add this string in libraries
$autoload['libraries'] = array('database');
And another way is you need to load libraries in the model
class Name_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_name()
{
$query = $this->db->get('name');
return $query->result();
}
}
Kindly write it as follow when your going to fetch only name(s) from your table,
public function get_name()
{
$query = $this->db->select('name')->get('your_table_name');
return $query->result();
}
It will work fine.

Codeigniter Getting an error of

I don't how to solve this error. While using the following controller and views in the CodeIgniter-3.0.0 Version and i change the baseurl also too http://localhost/code/
controllers:
Hello.php
<?php
class Hello extends CI_Controller {
var $name;
var $color;
function Hello()
{
$this->name = 'Andi';
$this->color = 'red';
parent::CI_Controller();
}
function you()
{
$data['name'] = $this->name;
$data['color'] = $this->color;
$this->load->view('you_view',$data);
}
}
?>
View:
you_view.php
Hello You!
<font color="<?=$color ?>"><?=$name?></font>
http://localhost/code/index.php/Hello/you
Got an error:
Fatal error: Call to undefined method CI_Controller::CI_Controller() in E:\phpprogram\code\application\controllers\hello.php on line 9
A PHP Error was encountered
Severity: Error
Message: Call to undefined method CI_Controller::CI_Controller()
Filename: controllers/hello.php
Line Number: 9
Backtrace:
You should extend CI_Controller not Controller in controllers/Hello.php
You need to replace Controller with CI_Controller.
Bookmark to your browser the user guide. It is something you will need to visit quite often. Most things are pretty well explained there.
Try this on your controller:
class Hello extends CI_Controller {
function hello()
{
parent::Controller();
}
function you()
{
$this->load->view('you_view');
}
}

Check for Sessions in Controller _constructor

I am new to Codeigniter and still trying to understand some basics.
I am building a registration/login system. everything good for now.
I am creating a controller to show the user info. Before that I want to check if the session existis and if user is logged in, if not I want to redirect to the site root.
class Myprofile extends CI_Controller {
function __construct()
{
$user_data = $this->session->userdata('user_data');
$user_data['logged_in'] = isset($user_data['logged_in']) ? $user_data['logged_in'] : null;
if($user_data['logged_in'] != 1){
redirect();
}
}
public function index(){
redirect("myprofile/info");
}
public function info(){
echo"here I'll ave my info";
}
}
The problem is the error I am getting. It looks like I it can't see the sessions in the construct part of the script.
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Myprofile::$session
Filename: controllers/myprofile.php
Line Number: 6
This would be the very best solution, otherwise I need to put the same code on all the functions.
Hope to hear some feedback help. Thanks
Perhaps the session library is not loaded. Add session class on the autoload array() located in application/config/autoload.php
eg.
$autoload['libraries'] = array('database', 'session', 'output');
class Myprofile extends CI_Controller {
function __construct()
{
$this->load->library('session');
$user_data = $this->session->userdata('user_data');
$user_data['logged_in'] = isset($user_data['logged_in']) ? $user_data['logged_in'] : null;
if($user_data['logged_in'] != 1){
redirect();
}
}
public function index(){
redirect("myprofile/info");
}
public function info(){
echo"here I'll ave my info";
}
}

error for inserting data in database in view in joomla

I want to insert a data in database but it show error
Fatal error: Call to a member function getform() on a non-object in
\components\com_enquiry\views\form\view.html.php
on line 9
my code is:
enqiry.php:
public function getform($data)
{
$db=JFactory::getDbo();
$db=$this->getDbo();
$query=$db->getQuery(true);
$reg=new stdClass();
$reg->name=$data['name'];
$reg->email=$data['email'];
$reg->phone=$data['phone'];
$reg->comments=$data['comments'];
$reg=$db->insertObject('#__enquiry',$reg);
}
and view.html.php:
public function display()
{
$this->msg = 'enquiry form';
$model=$this->getModel();
$data =$model->getform();
$this->assignRef('data', $data );
parent::display();
parent::display();
}
controller:
class enquiryController extends JControllerLegacy
{
public function display()
{
$vname=JRequest::getCmd('view','form');
JRequest::setVar('view',$vname);
JRequest::setVar('layout','default');
parent::display();
}
public function show()
{
$data['name']=$_POST['name'];
$data['email']=$_POST['email'];
$data['phone']=$_POST['phone'];
$data['comments']=$_POST['comments'];
$sname=JRequest::getCmd('view','thanx');
JRequest::setVar('view',$sname);
print_r($data);
}
}
could someone help me solve this error?
The problem is that your model is not loaded by the line:
$model=$this->getModel();
This either means that your model in enquiry.php is not named correctly
ComponentNameModelEnquiry
or that your view is not named enquiry.
You can try to specify which model you want, like
$model = $this->getModel('enquiry');

set_exception_handler in codeigniter - how to use?

I have a code similar to the following which is not working.
class Abc extends CI_Controller {
static function exception_handler(Exception $ex)
{
var_dump($ex);
echo $ex->getMessage();
exit;
}
function __construct()
{
parent::__construct();
set_exception_handler('exception_handler');
}
function Index()
{
throw new Exception('hehe');
}
}
I get
A PHP Error was encountered
Severity: Warning
Message: set_exception_handler() expects the argument
(exception_handler) to be a valid callback
How to use set_exception_handler in codeigniter
Since exception_handler is a function inside a class, it should be:
set_exception_handler(array('self','exception_handler'));
Or
set_exception_handler(array('Abc','exception_handler'));
Set_exception_handler always expects full name class, including the namespace and omits the "use instruction" for use others namespaces.
In your case the fullname is: 'CI_Controller:exception_handler'
Therefore the correct call is: set_exception_handler('CI_Controller:exception_handler');
If you had one namespace e.g. namespace App;
The correct call is: set_exception_handler('App\CI_Controller:exception_handler');
function Index()
{
function ExceptionHandler($e) use($this)
{
$this->load->view('error', $this->sharedData);
}
set_exception_handler('ExceptionHandler');
throw new Exception('hehe');
}
You should use method of the instantiated Object with access level is public.
final class MyException
{
/**
* Constructor
* trigger getErrorTypesFromDefinedConstant method
*/
public function __construct()
{
// Set global exception handler
set_exception_handler(array($this, 'globalException'));
}
public function globalException($e)
{
// Your code to handle exception
}
}

Resources