Welcome Guest, Not a member yet? Register   Sign In
Creating and using a library
#1

[eluser]dwlamb[/eluser]
For the first time, I am constructing a library for some functions that need to be accessed by more than one controller.

After reading 'Passing Parameters When Initializing Your Class' in the documentation I am unsure if I need to set-up initializing to accept parameters or if the function will accept a variable I pass to it when it is called from a controller.

For instance, I have these lines in a controller:
Code:
function edit_content {
  ...code...
  $data['url_block'] = $this->urls($result);
  ...code...
  
}

function urls($result){
  ...code...
  return $output;
}
The function urls processes the variable $result, which is a result_array(), returning the rows as a list of url's.

With moving the function urls to a library, would the syntax in the controller become?:
Code:
function edit_content {
  ...code...
  $data['url_block'] = $this->Urls_library->urls($result);
  ...code...  
}

From reading the documentation, this is what I have for defining my library:
Code:
file named Urls_library.php
<<<Start of file>>>>>
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Urls_library {

    public function make_urls($result)
    {
        if (count($result) > 0){
            ...code  
            return $output;
        } else {
            return;
        }
    }
}
Thanks for taking time to read this.
#2

[eluser]treenef[/eluser]
Looks OK to me, have you tried testing it?
#3

[eluser]dwlamb[/eluser]
It did not work. :roll:

I am loading the library through the controller with:
Code:
$this->load->library('urls_library');

I have tried with the index function and at the beginning of the function that makes a call for a function in the library.

This is the error message:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: User::$Urls_library

Filename: controllers/user.php

Line Number: 174
#4

[eluser]treenef[/eluser]
Post your user library code, and controller code specifically line 174.
#5

[eluser]dwlamb[/eluser]
I have tried to call the library using function index() of the controller user.php

Code:
function index() {
  $this->load->library('urls_library');
  if ($this->session->userdata('logged_in')) {
   redirect(...code...);
  } else {
   $this->load->view(...code...);
  }//if
}//index

...and at the start of the function which makes a call to the library on line 174
Code:
function edit_content() {
  if ($this->session->userdata('logged_in')){
   $this->load->library('urls_library');
  
   ...code...
  
   $urls_data = $this->Model_user->populate_urls($id,$this->session->userdata('user_id'));
   $data['url_pit'] = $this->Urls_library->make_urls($urls_data); //this is the syntax on line 174
   $this->load->view('view.php', $data);
  } else {
   redirect ("user/login");
  } //if...else
}//edit_recipe

This is the content of Urls_library.php. It is saved to application/libraries/. I checked permissions. They are 666.

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

class Urls_library {

public function make_urls($result)
{
  if (count($result) > 0){
   $output  = "<ul class=\"url_list\">\n";
   foreach( $result as $key ){
    $output .= "<li>".form_hidden('id', $key['id'])."<a >"
      .$key['link_text']."</a>\n";
    $output .= "</li>\n";
   }
   $output .="</ul>\n";
   return $output;
  } else {
   return;
  }
}
}

/* End of file Urls_library.php */

I am really stumped.
#6

[eluser]rm_beginner[/eluser]
i am very new in code_igniter try to add this for you to get all CI core instances.

$CI =& get_instance();

$CI->load->library('urls_library');


http://ellislab.com/codeigniter/user-gui...aries.html
#7

[eluser]treenef[/eluser]
$data['url_pit'] = $this->Urls_library->make_urls($urls_data); //

Make sure the call to the library is all lower case on this line:

$data['url_pit'] = $this->urls_library->make_urls($urls_data); //
#8

[eluser]dwlamb[/eluser]
I do not understand why this would make a difference but modifying all of the names to remove underscores (_) fixed the problem. Not simply function names but the class name and the library file.

No more error messages emanating from line 174.

New rule: No underscores in function names or library class name when creating a library.
#9

[eluser]CroNiX[/eluser]
Strange, my custom libraries use underscores in the filename, class name and function names with no problems. I think treenef had the right solution in his last post. You were capitalizing when it should be all lowercase. Did you try it the way he suggested?
#10

[eluser]treenef[/eluser]
Yes... removing the underscores was just a coincidence as you probably removed the capitals as well at the same time, thereby prompting you to conclude that the underscores was a problem.

It is not this. It was the fact that you had a capital 'U' when you called the library from your controller.

Read the documentation for further clarification.

I know there are some issues with names being capitalized depending on what OS you are using either linux or windows. If in doubt refer to the documentation.




Theme © iAndrew 2016 - Forum software by © MyBB