Welcome Guest, Not a member yet? Register   Sign In
how to work with library like this?
#1

[eluser]Unknown[/eluser]
Dear friends, i'm from Brazil and i started yesterday with CI, ther is a fantastic framework, but a have a question, some one can help me?

My website need to use a Youtube videos, so i have to use the embed code on some time, then a want to create a library to work on entire website, but a always get a error.

1) Can i call a library from a view?

my code:

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Classtest {

    function some_function()
    {
        echo "teste";
    }
}

?>

and a call it on a view

Code:
$this->load->library('Classtest');
$this->Classtest->some_function();

but a have this error:

A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Loader::$Classtest
Filename: views/home.php
Line Number: 145

Can anyone help me please?
Sorry for my english!
#2

[eluser]faxtion[/eluser]
I see you problem, try reading the manual first Creating Libraries

Code:
$this->load->library('Classtest');
$this->Classtest->some_function();


When calling your class function the library should be in lowercase

Code:
$this->load->library('Classtest');
$this->classtest->some_function();
#3

[eluser]Kosonome[/eluser]
I guess you cannot call a library in a view without getting the CI instance.
Code:
$CI =& get_instance();
$CI->load->library('Classtest');
$CI->classtest->some_function();
#4

[eluser]Unknown[/eluser]
Thanks, a little mistake, sorry...

@faxtion i read this manual first, but this kind of error happend!
#5

[eluser]ShiverCube[/eluser]
You cannot not call a library from within a view file using the $this reference. You must get the CI reference using get_instance() as pointed out by Kosonome.

However, you should instead wrap up the functions in a <a href="http://ellislab.com/codeigniter/user-guide/database/active_record.html">helper</a>, or do the processing in the controller and provide the output to the view file by using a variable. This will clear the view files from unnecessary PHP code.
#6

[eluser]Johan André[/eluser]
If the library is loaded in the controller you can call it in the view without using get_instance()...
#7

[eluser]ShiverCube[/eluser]
[quote author="Johan André" date="1263767225"]If the library is loaded in the controller you can call it in the view without using get_instance()...[/quote]
That is true. However, you will not be able to load a new library within a view using the $this reference. But if you load the library in the controller first, then you will be able to access the library just as you would do so in a controller.




Theme © iAndrew 2016 - Forum software by © MyBB