Codeigniter Controller cannot use function within helper - php

I have the controller
<?php
class Onetimescan extends CI_Controller{
public function index() {
#LOAD -->>>> LIB ###########
$this->load->library('session'); ## LOAD LIBS: SESSION
$this->load->helper('simpledom');
#LOAD -->>>> MODEL
$this->load->model('scanmodel');
$this->scanmodel->loadurlbasedonsessid($this->session->userdata('session_id')); // echo out inserted domain in loadurlbasedonsessid() func
$sss= $this->session->userdata('tld'); // echo $sss;
$siteToSearch = file_get_html($sss);
foreach($siteToSearch->find('form') as $element){
echo "<h1 style='color:red; '>".$element->action."</h1> <br />";
}
}
}
?>
I'm getting a fatal error of Call to a member function find() on a non-object on this line:
foreach($siteToSearch->find('form') as $element){
simpledom is a helper I loaded at the top (actually called simpledom_helper.php) which has a file_get_html defined like:
[http://pastebin.com/HaJtKfNb][1]
What am I doing wrong here? I tried definining the function like public function file_get_html{} but that threw errors.
I fixed this all by adding:
$prefix = "http://www.";
//echo "SSS is: ".$sss;
$siteToSearch = file_get_html($prefix.$sss);
FIXED>>
The url was trying to get_file_contents(somedomain.com) and was missing the http://www. as a fully qualified domain. This function get_file_contents seems to require the http://www.

Helper functions should be called like this since they are not objects:
$this->load->helper('simpledom');
$siteToSearch = file_get_html($sss);

Related

Fatal error: Call to a member function base_url() on a non-object Codeigniter

Fatal error: Call to a member function base_url() on a non-object
I am getting the above error when I redirect to the site from payment gateway, hence the head file containing all the scripts does not get loaded
I tried using $ci =& get_instance(); But still its giving the same issue
public function payment_success() {
$this->load->model("bidding_m");
$this->load->model("admin_setting_m");
$this->load->model("channel_partners_m"); //call model functions to update the tables
$this->load->config('payu_config', TRUE);
$this->config = $this->config->item('payu_config');
//echo base_url();
exit;
}
Your problem is this line:
$this->config = $this->config->item('payu_config');
I don't know if this code is in library / controller so I tried explain it for both:
Controller:
You rewrite whole config ($this->config = ...) with a single config item and your base_url config item is lost! So do it this way:
$config = $this->config->item('payu_config');
Library:
You are accessing config item, but don't have instance of codeigniter. If this code is in library, it should looks like this:
public function payment_success() {
$ci = & get_instance();
$ci->load->model("bidding_m");
$ci->load->model("admin_setting_m");
$ci->load->model("channel_partners_m");
$ci->load->config('payu_config', TRUE);
//if you have in this library variable $config
$this->config = $ci->config->item('payu_config');
echo base_url();
exit;
}
You could try loading the helper like so
Filename first letterupper case
Somelibraryname.php
<?php
class Somelibraryname {
public function __construct() {
$this->CI =& get_instance();
$this->CI->load->helper('url');
}
public function something() {
echo base_url();
// Or
echo $this->CI->config->item('base_url');
}
}

Passing parameter to controller always get "Page Not Found" - CodeIgniter

I try to pass a parameter to my controller functions from my views but I always get "Page Not Found".
I've been looking for any solutions possible for my problem from here, here, and here but I still couldn't find working solutions and I still don't know what could be the problem. I think I've set everything right, but it's not working. Please tell me if there's something I'm doing wrong.
Here is my controller: Controll.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Controll extends CI_Controller{
public function about($thing){
$case = "About Us";
$data['page'] = $case;
$data['p'] = $thing;
print($thing);
//$this->load->view('/header/header',$data);
$this->load->view('about_us',$data);
$this->load->view('/footer/footer');
}
public function mountain_destination($thing){
$data['page'] = "Mountain Destination";
$data['p'] = $thing;
//$this->load->view('/header/header',$data);
$this->load->view('mountain_destination',$data);
$this->load->view('/footer/footer');
}
}
?>
Here's my routes setting:
$route['(:any)'] = "controll/$1";
$route['default_controller'] = "controll";
$route['404_override'] = '';
And here's one of my script called by onClick that tries to call that controller function:
function goTab(thing,tab){
switch(thing){
case "about":
$body.load("<?php echo site_url('about'); ?>/"+$(tab).text().toLowerCase().replace(/ /g,''));
break;
case "mountain":
$body.load("<?php echo site_url('mountain_destination'); ?>/"+$(tab).text().toLowerCase().replace(/ /g,''));
break;
}
My script runs well and it produces right url. For example, I put parameter "myteam" into "goTab" function, it produces link like this:
"http://127.0.0.1/about/myteam". This link's supposed to call "about" function inside my controller and pass "myteam" as parameter. But instead it returns "Page Not Found". And when I try to call "about" function without any parameters like this: "http://127.0.0.1/about", I get missing argument error like this:
Severity: Warning
Message: Missing argument 1 for Controll::about()
Filename: controllers/Controll.php
Line Number: 22
Please help me.
Thanks in advance.
You're not passing it to your about method. You're looking for a method called myteam within your Controll controller.
Amend your Routes to this;
$route['about/(:any)'] = 'controll/about/$1';
$route['mountain_destination/(:any)'] = 'controll/mountain_destination/$1';
Hope this helps.
This answer of mine might also help you;
Codeigniter Menu from Database
You can create a variable and pass it within constructor and simply use that whenever you need as
class Controll extends CI_Controller {
private $thing;
public function __construct() {
parent::__construct();
$this->thing = $this->uri->segment(3);
}
public function about() {
$data['p'] = $this->thing;
echo $this->thing;
}
public function mountain_destination($thing) {
$data['p'] = $this->thing;
echo $this->thing;
}
}

function defined in helper not found in controller

I'm using a helper function to validate XML in Codeigniter.
My helper function is defined in xml_validation_helper.php and is as follows:
/**
* Function to generate a short html snippet indicating success
* or failure of XML loading
* #param type $xmlFile
*/
function validate_xml($xmlFile){
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->validateOnParse = true;
$dom->load($xmlFile);
if (!$dom->validate())
{
$result = '<div class="alert alert-danger"><ul>';
foreach(libxml_get_errors() as $error)
{
$result.="<li>".$error->message."</li>";
}
libxml_clear_errors();
$result.="</ul></div>";
}
else
{
$result = "<div class='alert alert-success'>XML Valid against DTD</div>";
}
return $result;
}
I'm using it in my controller (specifically in the index method) and that is as follows:
function index() {
$this->data['pagebody'] = "show_trends";
$this->load->helper("xml_validation");
$this->data['pokedex'] = display_file(DATA_FOLDER ."/xml/pokedex.xml");
$pokedexResult = validate_xml($this->data['pokedex']);
$this->data['gameSales'] = display_file(DATA_FOLDER . "/xml/sales.xml");
$gameSalesResult = validate_xml($this->data['gameSales']);
$this->render();
}
However, I keep getting a "Fatal error: Call to undefined function validate_xml() in C:\xampp\htdocs\project\application\controllers\show_trends.php on line 15 error, even though I can clearly load the file. I've even tried to move the function into the same file as the index method, but it still says it's undefined.
Why am I getting this error, even though this function is clearly defined?
Provided your helper is named the_helper_name_helper.php (it must end with _helper.php) and is located in the application/helpers you have to load the helper file using:
$this->load->helper('the_helper_name')
If you plan on using functions in this helper often, you better autoload it by adding 'the_helper_name' to the $config['helpers'] array in application/config/autoload.php
You must load libraries and helper files in contructor function
check it out
<?PHP
class controllername extends CI_Controller
{
public function __construct()
{
$this->load->helper("xml_validation");
}
public function index() {
$this->data['pagebody'] = "show_trends";
// $this->load->helper("xml_validation");
$this->data['pokedex'] = display_file(DATA_FOLDER ."/xml/pokedex.xml");
$pokedexResult = validate_xml($this->data['pokedex']);
$this->data['gameSales'] = display_file(DATA_FOLDER . "/xml/sales.xml");
$gameSalesResult = validate_xml($this->data['gameSales']);
$this->render();
}
}
?>

Call to undefined method CI_Controller::Controller()

I have got this controller:
class Start extends CI_Controller{
var $base;
var $css;
function Start()
{
parent::Controller(); //error here.
$this->base = $this->config->item('base_url'); //error here
$this->css = $this->config->item('css');
}
function hello($name)
{
$data['css'] = $this->css;
$data['base'] = $this->base;
$data['mytitle'] = 'Welcome to this site';
$data['mytext'] = "Hello, $name, now we're getting dynamic!";
$this->load->view('testView', $data);
}
}
it tells me in this line:
parent::Controller(); //error here.
Call to undefined method CI_Controller::Controller()
If I remove that line..I get an error for the next line that says..
Call to a member function item() on a non-object
How do I prevent such errors form happening?
If you're using CI 2.x then your class constructor should look like this:
public function __construct()
{
parent::__construct();
// Your own constructor code
}
read more in user guide
In CodeIgniter 2, the constructor is named __constructor and not the class name. So you need to call parent::__construct() instead of parent::Controller()
Here's an article that you can read that shows one major difference between CodeIgniter 1.x and CodeIgniter 2.x
http://ulyssesonline.com/2011/03/01/differences-between-codeigniter-1-7-2-and-2-0-0/
If you're running your Codeigniter project via Xampp or a similar server add the following code to the bottom of your config.php file in the following directory; ci_project/application/config/config.php
function my_load($class) {
if (strpos($class, 'CI_') !== 0) {
if (is_readable(APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php' )) {
require_once (APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php');
}
}
}
spl_autoload_register('my_load');
The above code would help to; load class in core folder.
I'm certain this works in the following setup; CI-3+, Xampp, Php5.6, and or 5.6+
Also, you can then decide to create and allow other classes to reference your own Controller (which extends the original CI_Controller) by creating a file named MY_Controller.php in the following directory: ci_project/application/core/ and adding the following code in it;
<?php
class MY_Controller extends CI_Controller {
}
?>
That way you can always just reference or extend the other Classes to your own Controller (MY_Controller) throughout the rest of the project e.g.
class Admin extends MY_Controller {
//your function here
}
I hope this helps.

Create Simple Codeigniter library

For my current project i decided to create a library for some common functionalities.
Ex : Login_check,get_current_user etc.
With my little knowledge i created a simple one but unfortunately its not working.
Here my library :
FileName : Pro.php and located in application/libraries
class Pro{
public function __construct()
{
parent::_construct();
$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->load->database();
}
function show_hello_world()
{
$text = "Hello World";
return $text;
}
}
?>
And i tried to load it on my controller :
<?php
class Admin extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library(array('session'));
$this->load->library("Pro");
}
function index()
{
echo($this->Pro->show_hello_world());
}
}
?>
I cant see any erros there...but i am getting a blank page.
Whats wrong with me ??
Thank you .
Edit : I got this error :
Call to a member function show_hello_world() on a non-object in C:\wamp\www\Project\application\controllers\admin.php on line 13
One thing I notice: remove the parent::__construct() from your library constructor, because it's not extending anything so has no parent to call.
Also, enable error reporting by setting the environment to "development" in index.php, and you might also want to raise the logging threshold to 4 in config/config.php so you log errors.
Try this simple test-case:
file Pro.php in application/libraries:
class Pro {
function show_hello_world()
{
return 'Hello World';
}
}
Controller admin.php in application/controllers
class Admin extends CI_Controller
{
function index()
{
$this->load->library('pro');
echo $this->pro->show_hello_world();
}
}
while your class name is capitalized, all your references to the library when loading it and using it should be lower case. you also do not need the constructor, as the other commenter mentioned.
so instead of:
echo($this->Pro->show_hello_world());
you should have:
echo($this->pro->show_hello_world());
I prefer the standard php autoloader approach, with this you dont need to change your classes at all, you can use your standard classes without modifications
say for instance you class is class 'Custom_Example_Example2' and is stored in libraries
in sub folders you can add this autoloader in the master index.php
make sure it is added below the defined APPPATH constant
//autoload custom classes
function __autoload($className) {
if (strlen(strstr($className, 'Custom_')) > 0 ||
strlen(strstr($className, 'Other1_')) > 0 ||
strlen(strstr($className, 'Other2_')) > 0) {
$exp = explode('_', $className);
$file = APPPATH.'libraries';
if(!empty($exp)) {
foreach($exp as $segment) {
$file .= '/'.strtolower($segment);
}
}
$file .= '.php';
require_once $file;
//debug
//echo $file.'<br />';
}
}
This will look for class calls matching the 'Custom_' prefix
and reroute them to the relative location in this case
you only need to define the base prefix not the sub folders / classes
these will be auto detected by this code
APPPATH.'libraries/custom/example/example2.php'
You can call it in the controller the standard php way
$class = new Custom_Example_Example2;
or
$class = new custom_example_example2();
You can modify the script to your liking currently it expects all folders and filenames in the library to be lowercase but you can remove the strtolower() function to allow multiple casing.
you can change the require once to echo to test the output by uncommenting this line and refresh the page, make sure you have a class init / test in the controller or model to run the test
echo $file.'<br />';
Thanks
Daniel
In Pro.php
class Pro{
protected $CI;
public function __construct() {
$this->CI = & get_instance();
}
public function showHelloWorld(){
return "Hello World";
}
}
In your controller
class Staff extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->database();
$this->load->helper(array('url_helper', 'url'));
$this->load->library("pro");
}
public function index() {
echo $this->pro->showHelloWorld();die;
}
}
Just do these things you can access your custom library in codeignitor.

Categories