I have extended CI's baked in form validation, however I'm having issues loading a language files. I have the following code:
class MY_Form_validation extends CI_Form_validation {
public function __construct() {
parent :: __construct();
$this->load->lang('raf');
}
However this throws the error:
Call to a member function lang() on a non-object
Can anyone spot the issue here?
To load language in codeigniter, Syntax is :
$this->lang->load('filename', 'language');
Not:
$this->load->lang();
Try after load Helper file
$this->load->helper('language');
Related
I am a beginner in CodeIgniter framework and I have problem with links. I found a couple of pages in which is explained how to link pages, but from some reason I have problem to link two pages. I did this:
Insert Labwork
and that should call controller_proffesor method :
function index(){
$this->load->view('proffesor_view_insert_labwork');
}
i try also this:
Insert Labwork
but when i click on that link this is what i get:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Controller_proffesor::$load
Filename: controllers/controller_proffesor.php
Line Number: 7
Backtrace:
i have also include this line in my autoload file :
$autoload['helper'] = array('url','form');
What am I doing wrong?
Within the constructor have you added:
parent::__construct();
Try adding it inside __construct(). It invokes the constructor of inherited parent class CI_Controller.
Your code should look like this..
class Controller_proffesor extends CI_Controller {
public function __construct()
{
parent::__construct();
//followed by your helper, model load statements
}
NOTE: Refer this https://stackoverflow.com/a/21339891/5176188 for more details. This may be a duplicate question.
Try this:
In your config file you must set something like this:
$autoload['helper'] = array('url','form','html');
In the controller, you must forget to extend it and the name also must concide with the name of your class and it is also like the model extends it to CI_Model
Class Class_Name extends CI_Controller {
public function __construct () {
parent::__construct
// you can load here the session and models
}
}
Hope it helps.
I was creating objects of a class in Codeigniter core classes which I will extend in my controllers but there seems to be problem with loading model after object is created. Here is what I did
Core File:
require_once('folder/systemize.php'); // class systemize is in this file
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
$systemize = new systemize();
$systemize->init();
}
After this object is initialized, loading model will not work
Controller file which extends MY_Controller
class Log extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('access_model', 'TEST');
$this->TEST->foo('param1', 'param2'); //function inside access_model
}
It will give me these errors
A PHP Error was encountered
Severity: Notice Message: Undefined property: Log::$TEST
Fatal error: Call to a member function foo() on a non-object
These errors are solved if I don't create object of systemize. Can anyone tell me the how to solve this problem?
EDIT: Systemize File
class Systemize extends CI_Controller
{
function __construct() {
parent::__construct();
}
public function init() {
if('2' == '3') // false for now so it does nothing
{
$this->load->view("system/view_file");
exit();
}
}
Php error indicates on Log File where TEST->foo is called
If I Load Model before creating object, it works but not vice-versa. I still don't understand why is Codeigniter behaving like this?
Ok after going through system files of Codeigniter and printing and echoing inside them I found the source of problem. Now the behavior of CI_Controller is that when we extend CI_Controller, it creates object of Controller and Model and stores it with reference to the pointer.
Now what I did is that I extended systemize with CI_Controller.
MY_Controller extends CI_Controller which means $obj is created storing pointer to all objects of CI which will be used.
But when I manually created object of systemize extending CI_Controller, it has created object of only Controller and not Model and replaced the $obj.
That is why loading model will cause problem since $obj doesn't have Model Object in it.
The solution the problem I found is I did not create object of systemize in MY_Controller. Instead I extended MY_Controller with Systemize and it started working flawlessly.
Another solution is if you don't need CI functionality in your class you can create object of class and not extend it with CI_Controller.
Why do I get this error?
Fatal Error
Error: Call to a member function find() on a non-object
File: /home/mycake/public_html/app/Controller/TasksController.php
Line: 7
I think it has something to do with using CAKE 2.0 but I think the code in my controller might be CAKE 1.3? And I have done a bit of research but I don't know how to change the code to be in CAKE 2.0. Can anyone help?
This is the TasksController.php page
<?php
class TasksController extends AppController {
public $name = 'tasks';
public function index() {
//THIS IS THE LINE 7
$tasks = $this->Task->find('all');
$this->set('tasks', $tasks);
}
}
If you need any more info please ask because I'm not sure how else to make it more relevant to get an answer :)
If you controller is called TasksController then it will try and instantiate the Task model automatically. You don’t need to manually specify it. The reason CakePHP is throwing an error is because you’ve pluralised the name (models are singular, so Task not Tasks) and are also camel-cased, meaning they start with an uppercase letter (Task not task).
CakePHP does auto instantiation of corresponding Model if the particular controller's property $uses is not set, so this means you might accidentally setting the property $uses of your AppController to either null or array().
So, check that if you have $uses in your AppController set to empty, or simply, in your TasksController, overwrite the value with public $uses = array('Task');.
You can try the index like this:
class TasksController extends AppController{
public function index(){
$this->set('tasks', $this->Task->find('all'));
}
If you get the Error: Call to a member function find() on a non-object, You can put:
$this->loadModel('Task');
So I'm trying to extend the input library (CI 2.1.1), and when I call my custom save query function, its saying the function doesn't exist.
File: MY_Input.php, in the applications/libraries folder:_
class MY_Input extends CI_Input {
var $CI;
function __construct() {
parent::__construct();
$this->CI =& get_instance();
}
function save_query($query_array) {
$this->CI->db->insert('ci_query', array('query_string' => http_build_query($query_array)));
}
}
And in the controller I'm calling the function like this
$query_id = $this->input->save_query($query_array);
So what on earth am I doing wrong that it's giving me this error:_
Fatal error: Call to undefined method CI_Input::save_query() in ....
Can't see why it's not working, I even checked the user guide and according to it I guess I'm doing it right. :/
The CI_Input class is a core library (new thing in CI2.0.0). You will have to put your MY_Input.php file under application/core/ to make the framework pick it up.
When in doubt, look for the original class under system/core or system/libraries and mirror it under application/.
i use ubuntu 10.10 and codeigniter 2.0.2
i have successfully installed CI by opening welcome index page
later on i was following tutorial and have added new controller to my project:
class Start extends CI_Controller{
var $base;
var $css;
function __construct() {
parent::__construct();
$this->base=$this->config->item('base_url');
$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);
}
}
as well as view(testview.php) and css variable in question. then upon trying to test it by executing http://localhost/ci/index.php/index/start/hello/fred i get 404 page not found.
thank you
use this class declaration instead
class Start extends CI_Controller{
and instead of your php4 constructor
use this instead of Start()
function __construct(){
parent::__construct();
$this->base=$this->config->item('base_url');
$this->css=$this->config->item('css');
}
The actual reason you're getting a 404 is because you're telling it to find a function called fred. The url you're probably meaning to hit is this...
http://localhost/ci/index.php/start/hello/fred
Since 2.0.x , Codeigniter has changed their base controller class names and moved everything to php5 style constructors, among other things.
You are probably following a older tutorial.
It seems you have used an old tutorial. In CodeIgniter 2, some things are different.
extend CI_Controller instead of extend Controller
Use __construct for constructors instead of the class name.
function __construct(){
parent::__construct();
// More stuff
}
The url should be http://localhost/ci/index.php/start/hello/fred. CodeIgniter's URLs are used like so:
http://localhost/ci/index.php/<controller>/<method>/<params>