So I have followed every rule there is on CI docs page but still get error
Call to undefined method CI_Form_validation::clear_field_data()
I placed the file MY_Form_validation.php in application/core directory:
class MY_Form_validation extends CI_Form_validation {
public function __construct()
{
parent::__construct();
}
public function clear_field_data() {
$this->_field_data = array();
return $this;
}
}
and then in my controller I load class as usual:
$this->load->library('form_validation');
and use my new method:
$this->form_validation->clear_field_data();
$this->load->view('product/create', $data);
Which are the mistakes in my attempt to extend the library?
Your own libraries must be in the folder: application/libraries
$this->load->library('form_validation');
$this->form_validation->clear_field_data();
$this->load->view('product/create', $data);
You're loading the wrong library? Your lib should also go in application/libraries.
$this->load->library('MY_Form_validation');
$this->MY_Form_validation->clear_field_data();
$this->load->view('product/create', $data);
Related
I have this function that is constantly being reused in my controllers. I have decided to move it into a file that can be referenced all the time. This is my file structure
controllers
|Generic
|Users
|get_all_languages.php
|Users
|Lang
|Lang.php
I want to reference get_all_languages which contains
<?php
function get_all_languages(){
$this->curl->create(GetAllLanguages);
$this->curl->http_login(REST_KEY_ID,REST_KEY_PASSWORD);
return json_decode($this->curl->execute(),true);
}
So far, I have tried including it in the top of my file like:
<?php
include __DIR__.'/../../Generic/Users/get_all_languages.php';
class Lang extends CI_Controller{
However, when I try to use that function like $this->get_all_languages();, an error occurs saying Call to undefined method Lang::get_all_languages()
I have also tried to include it after the __contruct but it doesn't allow me to compile.
I hope someone can let me know how I can reference that function.
Thank you.
You can use library or helper of codeigniter.
And you can load them automatically in application/config/autoload.php.(Reference it)
If you want the specific controller, you can use it in construct of controller using $this->load->library() or $this->load->helper().
For example:
class A extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('libraryname');
$this->load->helper('helpername');
}
public function index() {...}
...
}
...
Updated
application/helpers/global_lang_helper.php
<?php
function get_all_languages(){
$CI = &get_instance();
$CI->load->library('curl');
$CI->curl->create(GetAllLanguages);
$CI->curl->http_login(REST_KEY_ID,REST_KEY_PASSWORD);
return json_decode($CI->curl->execute(),true);
}
At your controller...
public function __construct()
{
parent::__construct();
$this->load->helper('global_lang');
}
I created a helper in CodeIgniter 3 which I called asset_helper.php
and this is the controller I use to call this helper:
<?php
class Index extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('asset');
}
function index()
{
$this->load->view('header');
$this->load->view('nav');
$this->load->view('title');
$this->load->view('stat1');
$this->load->view('footer');
}
}
but when I access to the controller I get this message:
Unable to load the requested file: helpers/asset_helper.php
How can I solve this problem?
Load like this
$this->load->helper('asset_helper');
If you use like this $this->load->helper('asset'); it will never works.B Bcz there is no file call asset
I have this file structure:
application
controllers
home.php
core
MY_Controller.php
models
users.php
These are the codes for each file:
home.php
class Home extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
echo "Hello World!";
}
}
MY_Controller.php
class MY_Controller extends CI_Controller {
var $user_model = null;
public function __construct() {
parent::__construct();
$this->user_model = new Users();
}
}
users.php
class Users extends CI_Model {
public function __construct() {
parent::__construct();
}
}
When I load the webpage on a browser, I get this error:
Fatal error: Class 'Users' not found in C:\%path%\application\core\MY_Controller.php on line 7.
Please help me make my custom base Controller to find my Model. Thank you!
NOTE:
The cases are as I have provided them (I heard there might be issues with case-sensitivities).
EDIT:
When I opened the log files, I found this.
ERROR - 2014-02-17 01:02:05 --> Severity: 8192 --> mysql_pconnect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead C:\%path%\system\database\drivers\mysql\mysql_driver.php 91
Nevermind, I got it to work by either going in the config.php file and autoloading the model or by doing $this->load->model('Users');
Upper or lower case U seems to work.
I am using Codeigniter with HMVC .
When I validate my form, I use the solution to fix the callback problem in HMVC :
1.Extends CI_Form_validation:
<?php
// application/libraries/My_Form_validation.php
class MY_Form_validation extends CI_Form_validation {
public $CI;
}
?>
In my Controller :
class User extends MX_Controller {
function __construct() {
parent :: __construct();
$this->load->library('form_validation');
$this->form_validation->CI = & $this;
}
My problem is: in my localhost, everything is ok: callback validation run normally. But when I upload my project on host, i execute my controller it appears blank. When I comment the line
//$this->form_validation->CI = & $this;
The controller run normally again.
Could you tell me how i fix this problem?
Thanks for any help.
Check your error logs for any fatal errors, also you should extend my form validation this manner
class MY_Form_validation extends CI_Form_validation {
private $CI
function My_Form_validation($config = array()) {
parent::__construct($config);
$this->CI = &get_instance()
}
//example functions
function my_custom_validation(str){ //or override , for example, required
if($this->CI->method_from_instance()){
return true;
}
return false
}
}
and then use form_validation as you normally do, but now you have extra rule my_custom_validation
EDIT: if by any chance and for some wired reason you want to use controller functions in your library (aka $this) pass it as a config, once you load the library e.g. $this->load->library('form_validation', array(('ci'=>$this) //but i don't see why would you do that.
I'm using CodeIgniter (v1.7.2) and I've created a custom controller that incorporates authentication called MY_Controller (based on David Winter's blog post). When I load any controllers that use this base class, I get this error;
*Message: Undefined property: MY_Controller::$session*
Note that I am autoloading 'session' (and 'MY_controller' as a library) like so:
$autoload['libraries'] = array('database', 'session', 'MY_Controller');
Here is MY_Controller:
class MY_Controller extends Controller {
public function __construct() {
parent::__construct();
if (!$this->session->userdata('loggedin')) { <-- error is here
header('Location: /sessions/login');
exit();
}
}
}
Here's the controller that I'm trying to load:
class Welcome extends MY_Controller {
function __construct() {
parent::__construct();
}
function index() {
$this->load->view('header');
$this->load->view('welcome_message');
$this->load->view('footer');
}
}
When I var_dump $this->session above where the error occurs, I can see that it is NULL. Even putting $this->load->library('session'); in MY_Controller's constructor doesn't work. Why isn't it loading properly?
Thanks
Try taking MY_Controller out of the autoload.
$autoload['libraries'] = array('database', 'session');
You are extending the controller class which is automatically loaded by codeigniter as it is part of the core