CodeIgniter linking to another page/controler - php

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.

Related

Can't find model in CodeIgniter controller

I load the Get_notes model in line 8, but when I want to use Get_notes model to load add_notes method in line 23, the error occur and say Undefined property: Notes::$Get_notes in line 23 !
There is something wrong in line 23 but I dont know what is that.Please help me.
Thanks
<?php
class Notes extends CI_Controller
{
public function index()
{
$this->load->model('Get_notes');
$data['notes'] = $this->Get_notes->get_mm();
$this->load->view('show', $data);
}
public function insert()
{
$title = 'Something';
$text = 'Something else';
$data = [
'title' => $title,
'text' => $text
];
$this->Get_notes->add_notes($data);
}
}
The way you've written it, your Get_notes Model is only available within your index() function.
To make your Get_notes Model available to all functions within a single Controller, load it within that Controller's constructor function...
class Notes extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('get_notes'); // available to all functions within Notes
}
....
To make your Get_notes Model available globally to all functions in any CI Controller, put it in your $autoload['model'] array within the autoload.php file located at application/config/autoload.php...
$autoload['model'] = array('get_notes'); // available to all functions in your CI application
→ Note that it's supposed to be written in all lower-case no matter how you reference it later.
$this->load->model('get_notes');
$this->get_notes->add_notes($data);
See:
Class Constructors
If you intend to use a constructor in any of your Controllers, you MUST place the following line of code in it:
parent::__construct(); // Notice there is no dollar sign
Anatomy of a Model:
Where Model_name is the name of your class. Class names must have the first letter capitalized with the rest of the name lowercase. Make sure your class extends the base Model class.
Loading a Model:
Your models will typically be loaded and called from within your controller methods. To load a model you will use the following method:
$this->load->model('model_name');
Once loaded, you will access your model methods using an object with the same name as your class:
$this->model_name->method();
Auto-loading Resources
To autoload resources, open the application/config/autoload.php file and add the item you want loaded to the autoload array. You’ll find instructions in that file corresponding to each type of item.
An codeigniter model call is case sensitive therefore to get an model you should use
$this->get_notes->a_function_inside_the_model ();
Also note that the name inside the model folder should always start with a uppercase.
Whenever we run codeigniter on localhost servers like wamp server these problems dont exist but on a live server they will.
Hope this was helpful
Put $this->load->model('Get_notes'); in the insert() function.
If you want to use it globally, put it in a constructor.
Your get_notes model is only loaded in the index function. You cannot load the model in the index function and use it in any other function without having to load it again.
I'm thinking you're trying to load the model once and use it throughout your controller. To do this, you have to load it in the __construct method instead. Your code should be similar to this:
<?php
class Notes extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Get_notes');
}
public function index()
{
$data['notes'] = $this->Get_notes->get_mm();
$this->load->view('show', $data);
}
public function insert()
{
$title = 'Something';
$text = 'Something else';
$data = [
'title' => $title,
'text' => $text
];
$this->Get_notes->add_notes($data);
}
}

Get the constructor of class that inherits

I have this class in /libraries.
class CI_Decorator extends CI_Controller {
public function __construct() {
parent::__construct();
}
}
The error I get is very weird:
Message: Undefined property: CI_Decorator::$load
Filename: libraries/Form_validation.php
Backtrace:
File: /application/libraries/CI_Decorator.php
And is because I try to get CI_Controller constructor with
parent::__construct();
UPDATE:
I am trying to implement this Decorator but I think was developed for an earlier version of CI.
Change the file name to Decorator.php and in the class file use this.
class Decorator{
//there is no parent so you cannot call a parent constructor
//public function __construct() {
//parent::__construct();
//}
}
Only preface a file (or class) name with "CI_" when you wish to replace one of CodeIgniter's core classes. If a core class with that name does not exist the load will fail.
If you want to extend a native class then you will preface the class and file name with MY_ or with the prefix you set in config.php
$config['subclass_prefix'] = 'YOURPREFIX_';
Extending CI_Controller more than once is frustrating too and you will run into a different but related problem. You will find many answers to that question here on Stack Overflow. In the search box above use the search term "[codeigniter] extending CI_controller" (without the quote marks)

How to run code before all my methods inside controller in Laravel 4?

I have Content controller with REST methods (index..create..store..) and i want to run some code before any of those methods run.
what i am trying to do is to set var for my layout with some data that is relevant to all my methods within Content controller:
$this->layout->myvar = 'some-data';
I tried to do something like that:
class ContentController extends BaseController {
function __construct() {
$this->layout->myvar= 'some-data';
}
..
but it doesn't seems to work.
i get "Attempt to assign property of non-object" error.
Laravel 5.1+
This has been deprecated in favour of Middleware.
Laravel 4
You could set the beforeFilter like this:
class ContentController extends BaseController {
function __construct() {
// this function will run before every action in the controller
$this->beforeFilter(function()
{
// this will make the variable $myvar available in your view
$this->layout->with('myvar', 'some-data');
});
}
}
try share in app/routes.php
View::share('variable_name', 'value');
ex:
View::share('name', 'Steve');
will share variable with its value across all views

Changed controller load locations, and can't use any functions in controllers

By default, as many of you probably know, codeigniter loads controllers from application/controllers (URI Segment 0). Well... I changed it so that it loads them fom application/modules/(URI Segment 0)/controllers/(URI Segment 1 / not set: URI Segment 0). Up to here it all works fine, but when I create a controller, I cannot use any of the default function as $this->load or etc... Example:
/**
* Home Controller Class
*/
class Home extends CI_Controller
{
function __construct()
{
$this->load->database();
}
public function index()
{
echo "Testing...";
}
}
This example would always result in an error:
Severity: Notice
Message: Undefined property: Home::$load
Filename: controllers/home.php
Line Number: 10
Well I don't know how you exactly changed the Controller location.
But when you extend the CI_Controller, and you use a constructor method, you need to execute the parent's __construct() method at first, as follows:
class Home extends CI_Controller
{
function __construct()
{
/* Execute the CI_Controller constructor */
parent::__construct();
$this->load->database();
}
}
That's because your local constructor will be overriding the one in the parent controller class. So you need to manually call it.
CodeIgniter Doc.
Similar topics on SO:
Undefined $load property error after upgrading CodeIgniter 1.7 to 2.1
Codeigniter will not load language

Why do I get this error - Error: Call to a member function find() on a non-object - Cake 2.0

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');

Categories