Unable to access library function in codeigniter - php

I am noob in codeigniter. I used an external class file for getting the length of an audio file. I put the file in application/libarary directory. After googling and reading documentation I tried two things for accessing the libarary function which I have mentioned under the codeigniter problem.
Working Demo from the External Library File
require ('classAudioFile.php');
$filename = "40f11852c2314cdf1d06308f07de3b1c.mp3";
$AF = new AudioFile;
$AF->loadFile($filename);
echo $AF->wave_length;
CodeIgniter Problem
$filename = "./uploads/audio_files/347f21502e9b27fac4707b07c3c15eb7.mp3";
$CI =& get_instance();
$CI->load->library('AudioFile');
$CI->AudioFile->loadFile($filename);
$filename = "./uploads/audio_files/347f21502e9b27fac4707b07c3c15eb7.mp3";
$this->load->library('AudioFile');
$this->AudioFile->loadFile($filename);
I am getting Call to a member function loadFile() on a non-object in D:\wamp\www\webcartz\application\modules\playlist\controllers\admin.php on line 149 this is the actuall error.. can any body please help. I tried to call it in both Controller and View file.

to normalize everything try the following:
the AudioFile.php you put in the library folder, make the file name all lower case, like audiofile.php. if it is called classAudioFile.php, rename it to simply audiofile.php
check that there is a class AudioFile in audiofile.php.
change the AudioFile in the load->library to all lowercase
change the AudioFile in the $this->AudioFile-> to all lowercase
if it still does not work change the name of the class (not the file) to Audiofile, capital A, lower case f.
Also, there should be an default - no parameter constructor in the class Audiofile. like this:
public function __construct()
{
// you can put some code here if you want
}

Related

can't bind href with php function

I'm working on a new website. This website will be a one pager. All my files I already load in through PHP into the main folder. But now I want to edit them and update them through WYSIWYG.
The UPDATE and SELECT are already working. I tested it on a page who stood on its own. All the one-pager files are stored in a folder and within the folder is also the file which loads all the files into it. I call them through a href which ends up giving the file an # in the address bar. There lies the problem. I can't access the #file with the function I wrote because that only can access files without starting a #. Is it possible to access it through my function?
I give the code if the question is too unclear because it's a bit of mess to implement all the files I use for this purpose.
Short recap: Can't access #domain with a PHP function. Is it even possible to access it.
<?php
class Home extends Controller {
protected function frontpage() {
$viewmodel = new HomeModel();
$this->returnView($viewmodel->frontpage(), true);
}
}
?>
this is the returnView from Controller:
protected function returnView($viewmodel, $fullview){
$view = 'views/'. get_class($this). '/' . $this->action. '.php';
if($fullview){
require('views/main.php');
} else {
require($view);
}
}
I'm new with overflow so I couldn't get the function into right place but with protected function frontpage I should access the file frontpage.php. Well it does do that but that's not the right directory because it's only visible throug a href which means the function should have been: protected function #frontpage which isn't possible.
This is actually not possible, as the anchor (#) isn't sent to the server and is handled by the browser itself.
The only possibility is to include some JavaScript magic to your project.

What are possible reasons for bonfire Assets::add_module_css to not include file?

I have a primary controller located in secure/application/modules/gps/controllers that has a constructor that looks like this:
public function __construct()
{
parent::__construct();
$this->load->model('gps_model');
Assets::add_module_js('gps', 'gps.js');
Assets::add_module_css('gps','gps.css');
if($this->input->get('clear') != false){
$this->session->sess_destroy();
}
}
My CSS file that I am trying to include is located in the folder secure/application/modules/gps/assets/css. The code executes fine without warning, but the CSS file does not get included for any methods. Is there a configuration setting the may override the assets directory, or is there some other reason it's not being found/added? (The JS file is not being added either. The bonfire base CSS files (screen.css) IS getting loaded fine.
We found the solution to our particular problem.
The assets/cache directory did not exist. Once the server could write (it must exist and be writeable!) to the [document_root]/bonfire/public/assets/cache directory all was good.
This is not a solution, but I have faced the same problem and found help with the below information.
It'll added in your page but can you just look on ctrl+u source where bonfire will auto rename your file.
For example : In my code I have added id_proof_master.css file like below.
public function __construct()
{
parent::__construct();
$this->auth->restrict($this->permissionView);
$this->load->model('id_proof_master/id_proof_master_model');
$this->lang->load('id_proof_master');
$this->form_validation->set_error_delimiters("<span class='error'>", "</span>");
Template::set_block('sub_nav', 'master/_sub_nav');
Assets::add_module_js('id_proof_master', 'id_proof_master.js');
Assets::add_module_css('id_proof_master', 'id_proof_master.css');
}
And it's working fine but when I have check in source view (ctrl+u) it will show file name like "id_proof_master_master_mod.min.css" so can you just check it out in source; maybe it'll show you with some other name like my file.

PHP Autoload - File Found - Class Fatal Error

I have a function on load that checks a users login status. Here is a small chunk of the code...
//
// user is logged in, get user info
$user = Model\User::getByKey($_COOKIE['ukey']);
if($user->field('id') >= 1 || !isset($status)) {
//
// establish google connection
Classes\Google\Google::construct($user->field('id'), $user->field('ga_account'));
...
I have an __autoload function which finds the path of the file perfectly fine. However, I get a fatal error because the "\Classes\Google\Google" class is not found.
new Google() ... is located at /classes/google/google.class.php. The file is there, the autload function has the correct file path but why isn't the class found?
I do not have any namespacing on this particular Google() class file. This is all custom built, not a third party framework.
1- use class_exists to make sure class is loaded .
2- if class not loaded , in google.class.php make a die , so u'll know file loaded or not.
3- if class loaded , make a new object here . the test it , so u can find the class definition is true or not.

Joomla: Call helper function from within a model?

I'm starting off with both php and Joomla development, and finding it difficult working within Joomla to do some fairly simple stuff. Went through the Joomla MVC example and Lynda (and have built a few simple views so far).
I have a helper file/class/function that outputs all the userids that exist in the "completed" table so I can display a link for either a new record based on that user or edit an existing user's record.
I've already used a different function in this helper file successfully in a different part of the component ( Joomla: Write and call a helper function in a component ).
When I do the same thing in the model, I'm getting this: "Fatal error: Call to protected method JModel::_createFileName() from context 'JView' in C:\wamp\www\ilplocal\libraries\joomla\application\component\view.php on line 773". When I try it in the view, works fine - but I need the output in the model.
Code:
lookups.php
abstract class LookupHelper {
public function other_functions($vars){
...
}
public function completions_exist() {
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->SELECT(' #__completed.completed_userid as UserID');
$query->FROM (' #__completed');
$query->GROUPBY (' #__completed.completed_userid ');
$db->setQuery($query);
$result = $db->loadResultArray(0);
return $result;
}
}
In the model:
$completions_exist = Jview::loadHelper('lookups');
$completions_exist = LookupHelper::completions_exist();
This line is throwing the error: $completions_exist = Jview::loadHelper('lookups');
I've found some really vague references to something called JLoader::register to pull in helper functions but can't find any good documentation on that in Joomla except for everyone saying to just use that. SO I tried using it like so:
JLoader::register('LookupHelper', dirname( JPATH_COMPONENT_ADMINISTRATOR).DS.'helpers'.DS.'lookups.php');
$completions_exist = LookupHelper::completions_exist();
which throws this error: "Fatal error: Class 'LookupHelper' not found in C:\wamp\path\to\model\not\to\lookups.php. Tried manipulating the JLoader::register(everything here) and it doesn't effect the path of the error message.
Thoughts? Why does it work in a view and not in the model? How do I use the helper functions within a model?
Thanks!
#####EDIT
Thanks to #cppl looks like it's a path issue with the second bit of code. Also I read that the .DS. notation will be phased out in future versions - so the code that's working is:
JLoader::register('LookupHelper', JPATH_COMPONENT_ADMINISTRATOR.'/helpers/lookups.php');
$completions_exist = LookupHelper::completions_exist();
Lets break this down:
In Joomla! your components helper file should be in `/mycomponent/helpers/lookup.php'
JLoader:: is the Joomla! way to do it, but you could just as easily use PHP's require_once eg. require_once JPATH_COMPONENT_ADMINISTRATOR.'/helpers/myfunctions.php';
Is your path right? - you're providing dirname(JPATH_COMPONENT_ADMINISTRATOR).DS.'helpers'.DS.'lookups.php' but you've wrapped the path to your component in dirname which will the parent element of the path only. So JLoader is looking in /administrator/helpers/lookups.php.
JPATH_COMPONENT_ADMINISTRATOR is initialised as part of Joomla!'s renderComponent() call in it's JComponentHelper class if you apply dirname to it when it's not setup you will get back a dot (ie. current directory) so in the model you could would be passing ./helpers/lookups.php to the JLoader call.
You can call helper within model by following method:
JLoader::import('helpers.events', JPATH_COMPONENT);
this will include the file helpers/events.php from the component directory.
$_helper = new EventsHelper();
echo $_helper->getAnyInsideMethod();exit;

Codeigniter controller created when image not found

I have a problem in CodeIgniter, and that is that when an image is not found on the server, the instance of a controller is created (besides the one that called the view).
I know all this can sound confusing, so this is the code to observe what I'm saying. I did this changes to a clean 2.1.0 CI version:
Add a controller to override the 404 error page, I added this one:
// add application/controllers/Errors.php
Class Errors extends CI_Controller {
public function error_404() {
echo 'error';
}
}
// change routes.php
$route['404_override'] = 'Errors/error_404';
Use a controller that isn’t the default one with an unexisting image, I used this:
// add application/controllers/Foo.php
Class Foo extends CI_Controller {
public function index() {
echo '<img src="doesntexist.png" />';
}
}
I couldn’t figure out another way of debugging it, so I created a log to write the events on CodeIgniter.php:
// add on CodeIgniter.php line 356
$path = 'log.txt'; //Place log where you can find it
$file = fopen($path, 'a');
fwrite($file, "Calling method {$class}/{$method} with request {$_SERVER['REQUEST_URI']}\r\n");
fclose($file);
With this, the log that generates visiting the index function is the following:
Calling method Foo/index with request /test/index.php/Foo
Calling method Errors/error_404 with request /test/index.php/doesntexist.png
Which is the problem I have, an instance of the Error class is created.
that is that when an image is not found on the server, the instance of a controller is created
Not really. What I believe is happening is that, since you're using a relative path for the image (and calling it directly inside a controller, which is wrong because you're ouputting something before headers), your browser attach the image directly to the CI url, thus making this request to the server:
index.php/doesntexist.png
Which is (correctly) interpreted by CI as a request to a controller, which doesn't exists, and therefore it issues the error class.
You could do, in your actual code (I'd put the images in a view, though):
echo '<img src="/doesntexist.png" />'
using an absoluth path, or using the base_url() method from the url helper:
echo '<img src="'.base_url().'doesntexist.png" />
This should tell the server to fetch the right request (/test/doesntexist.png) and won't trigger that error.

Categories