I don't how to solve this error. While using the following controller and views in the CodeIgniter-3.0.0 Version and i change the baseurl also too http://localhost/code/
controllers:
Hello.php
<?php
class Hello extends CI_Controller {
var $name;
var $color;
function Hello()
{
$this->name = 'Andi';
$this->color = 'red';
parent::CI_Controller();
}
function you()
{
$data['name'] = $this->name;
$data['color'] = $this->color;
$this->load->view('you_view',$data);
}
}
?>
View:
you_view.php
Hello You!
<font color="<?=$color ?>"><?=$name?></font>
http://localhost/code/index.php/Hello/you
Got an error:
Fatal error: Call to undefined method CI_Controller::CI_Controller() in E:\phpprogram\code\application\controllers\hello.php on line 9
A PHP Error was encountered
Severity: Error
Message: Call to undefined method CI_Controller::CI_Controller()
Filename: controllers/hello.php
Line Number: 9
Backtrace:
You should extend CI_Controller not Controller in controllers/Hello.php
You need to replace Controller with CI_Controller.
Bookmark to your browser the user guide. It is something you will need to visit quite often. Most things are pretty well explained there.
Try this on your controller:
class Hello extends CI_Controller {
function hello()
{
parent::Controller();
}
function you()
{
$this->load->view('you_view');
}
}
Related
I added a simple library/class to my application called Assets. The file is located at:
application/libraries/Assets.php
The content is:
class Assets {
public function get_images() {
....
}
}
In my controller I have:
function __construct() {
parent::__construct();
$this->load->library('assets');
}
And I call it using:
public function members() {
$data = array();
$data['images'] = $this->assets->get_images();
}
But I am getting this error:
An uncaught Exception was encountered
Type: Error
Message: Class 'CI_Assets' not found
Filename: ../system/core/Common.php
Line Number: 196
What could be causing this?
Try change class name
CI_Assets
Try this
$this->load->library('Assets');
$assets = new Assets();
$assets->get_images();
I am new to Codeigniter and still trying to understand some basics.
I am building a registration/login system. everything good for now.
I am creating a controller to show the user info. Before that I want to check if the session existis and if user is logged in, if not I want to redirect to the site root.
class Myprofile extends CI_Controller {
function __construct()
{
$user_data = $this->session->userdata('user_data');
$user_data['logged_in'] = isset($user_data['logged_in']) ? $user_data['logged_in'] : null;
if($user_data['logged_in'] != 1){
redirect();
}
}
public function index(){
redirect("myprofile/info");
}
public function info(){
echo"here I'll ave my info";
}
}
The problem is the error I am getting. It looks like I it can't see the sessions in the construct part of the script.
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Myprofile::$session
Filename: controllers/myprofile.php
Line Number: 6
This would be the very best solution, otherwise I need to put the same code on all the functions.
Hope to hear some feedback help. Thanks
Perhaps the session library is not loaded. Add session class on the autoload array() located in application/config/autoload.php
eg.
$autoload['libraries'] = array('database', 'session', 'output');
class Myprofile extends CI_Controller {
function __construct()
{
$this->load->library('session');
$user_data = $this->session->userdata('user_data');
$user_data['logged_in'] = isset($user_data['logged_in']) ? $user_data['logged_in'] : null;
if($user_data['logged_in'] != 1){
redirect();
}
}
public function index(){
redirect("myprofile/info");
}
public function info(){
echo"here I'll ave my info";
}
}
My custom core Class in application/core/MY_Controller.php
class MY_Controller extends CI_Controller{
protected $data = array();
function __construct(){
parent::__construct();
}
function rander_page($view){
//do this to don't repeate in every controller
$this->load->view('includes/header');
$this->load->view('top_menus');
$this->load->view($view, $this->data);
$this->load->view('includes/footer');
}
}
Index Controller in application/controllers
class Index extends MY_Controller{
function __construct(){
parent::__construct();
}
function index(){
$this->render_page('index');
}
}
Error is :Fatal error: Call to undefined method Index::render_page() in D:\wamp\www\ci\application\controllers\index.php on line 10
i am trying to use one controller for all pages help plz
Yes, the error is the clue itself, it has mentioned the function is undefined. You have not defined render_page function. Instead, you misspelled and named it as rander_page().
May be you are trying to write:
function render_page()
as you have called it like :
$this->render_page('index');
You have misspelled the function name. Call $this->rander_page('index');
I'm new to CI + MX and I tried the Modules::run(); style but I can't seem to let it work.
Here's my directory structure:
/application
-/modules
--/main
---/controllers
----main.php
---/models
---/views
--/connections
---/controllers
----connections.php
---/models
----/group_model.php
---/views
----connection_view.php
main.php controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends MX_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
}
function index(){
echo modules::run('connections/load_connections');
}
}
?>
connections.php controller:
<?php
class Connections extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->model('connections/group_model');
}
function load_connections(){
$user_id = 2;
$data['tabs'] = $this->group_model->get_groups($user_id);
$this->load->view('connection_view', $data);
}
}
?>
group_model.php model:
class Group_model extends CI_Model{
function __construct(){
parent::__construct();
}
/**
* Get all groups in db
**/
function get_groups($user_id){
$this->db->select('g.group_name');
$this->db->from('groups AS g');
$this->db->join('members AS m', 'g.group_id = m.group_id');
$this->db->where('m.user_id', $user_id);
return $this->db->get()->result_array();
}
}
My connection_view.php view contains a div and some php codes to display the $data['tabs'] passed as array in load_connections function.
The problem is, I'm getting an error that says:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Connections::$group_model
Filename: controllers/connections.php
Line Number: 14
and
Fatal error: Call to a member function get_groups() on a non-object in C:\xampp\htdocs\hmvc\application\modules\connections\controllers\connections.php on line 14
I have clearly followed all the instructions provided on MX wiki at https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home
and set up everything as needed.
My database.php under /application/config is already configured.
routes.php is also configured pointing the default controller to main.php
I wonder what I have missed or have done wrong that I can't get it to work.
Codeigniter version: 2.1.3
MX version: 5.4
almost missed it, extend connections with MX_Controller since you are calling modules::run(). Whenever you want a controller to be called with modules::run(), you extend it with MX_Controller instead of CI_Controller
Your second error is caused because of your first error.
Your first error is most likely caused because it cant open the group_model.
try this
$this->load->model('group_model');
So I'm getting the error: Undefined property: badge_progress::$bp_model.
I don't understand what's going on. Here is my code:
Controller:
<?php
// Badge Progress Module
class badge_progress extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('bp_model');
$data['dotpoints'] = $this->bp_model->dotpoints('1');
$this->load->view('bp_view', $data);
}
}
?>
Model:
<?php
class bp_model extends CI_Model {
function dotpoints($badge_id) {
$query = $this->db->query("SELECT * FROM course_topic_dotpoints WHERE badge_id = ".$badge_id);
if ($query->num_rows() > 0) {
return $query->result();
}
}
}
?>
Ah fixed it! Didn't realise that the main controllers (controllers outside of the module directory) also needed to be extending "MX_Controller" instead of "CI_Controller".
Class names must begin with an uppercase letter.
class Badge_progress extends...
class Bp_model extends...
http://codeigniter.com/user_guide/general/controllers.html
http://codeigniter.com/user_guide/general/models.html
update:
You shouldn't have the logic you need as a function in your constructor. Create a separate function to process the dotpoints stuff.
<?php
// Badge Progress Module
class Badge_progress extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('bp_model');
}
function dotpoints()
{
$data['dotpoints'] = $this->bp_model->dotpoints('1');
$this->load->view('bp_view', $data);
}
}
Also, you are missing the constructor in your model. Check out those links I posted earlier...