Codeigniter 3 - Problems Loading Dynamic Data from Model to Controller - php

Okay. So I have been stumping myself on where I have gone wrong with this. I am not-so-new to PHP but I am also no expert, although I am getting more and more used to OOP. I have recently got into CodeIgniter 3. I like it, I use it, it's pretty simple to grasp in the beginning.
However, I am in the learning stage where I cannot successfully pass dynamic data, from the database into the controller using a Model.
And just as an FYI: I am using MY_Controller as my Base Controller, and have set a $data object to pass variables application wide. I know my problem is not due to the use of MY_Controller, because when I use static $this->data['KEY'] = 'VALUE'; or $this->data array('KEY' => 'VALUE');, my variables get passed into my views. So MY_Controller is functional.
My problem resides in creating a Settings_model. Its sole purpose is to take data from a database table settings and extrapolate certain columns of data, such a/an: application title, background color, etc.
So. On to my question/s, and what I have tried to do to fix this.
First, here is my MY_Controller:
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public $data;
public function __construct() {
parent::__construct();
$this->data = array(
'app_title' => $this->settings_model->get_title('$title'),
'meta_author' => APP_AUTHOR
);
}
}
And here is my Settings_model:
defined('BASEPATH') OR exit('No direct script access allowed');
class Settings_model extends CI_Model
{
public $title = '';
public function __construct() {
$this->load->database();
}
public function get_title($title) {
$this->db->select('app_title');
$this->db->from->('settings');
$this->db-where('app_title', $title);
return $this->db->get()->row('app_title');
}
}
My Problem
When using the sytnax above. i am able to see my applications output in the browser. Everything is rendered properly, all the static variables I've assigned in MY_Controller work. Everything is good ... except, the $app_title variable (which is in my views, and assigned in MY_Controller) does not load the data from the database, as the model instructed. As well as does not produce any error, warning or notice.
What I've tried
Man i tell you I have tried everything. i have tried inserting the query into MY_Controller directly and not using a Model (which I know is not proper MVC, but I wanted to try), like so:
MY_Controller Using Straigh Query, no model
$this->data = array(
'app_title' => $this->db->query('GET app_title FROM settings'),
'meta_author' => APP_AUTHOR
);
And the one time i got somewhere, error/notice wise was when i altered the Settings_Model, like so:
Altered Settings Model
defined('BASEPATH') OR exit('No direct script access allowed');
class Settings_model extends CI_Model
{
public $title = '';
public function __construct() {
$this->load->database();
}
public function get_title($title) {
$this->db->select('app_title');
$this->db->from->('settings');
$this->db-where('app_title', $title);
// Before it was this:
return $this->db->get->row('app-title');
// But I changed it to this
return $this->db->get()->row()->app_title;
}
}
But that syntax above gave me the error in this Image of a non Object Error
I do not know what I am doing wrong.
Now the question/s
Why when i use the first approach of the settings_model, using return $this->db->get()->row('app_title'); does it show all of my applications output in the browser, but everywhere the $app_title variable is called, it itself displays no value from the database, via MY_Controller and Settings_model?
Why when i use the second approach of the settings_model, using return $this->db->get()->row()->app_title; does it produce the Trying to get property of Non Object error in the image i linked to above?
What on earth am I doing wrong? I know I am still on the early learning stages of OOP, and Models, etc. But I must be missing something, I know something is not right.
Any and all help is greatly appreciated and welcomed. I thank everyone for their input.
EDIT 1: Thanks to #Mirceac21 for asking. I am autoloading the settings_model in config/autoload.php.
EDIT 2: I have now switched the var $data to public $data in MY_Controller as #Mirceac21 suggested. Still the same error but he is correct about not using var there.

Okay, so I figured it out. With the help of #Mirceac21. I change the var $data to public $data in MY_Controller.
Then in the Settings_model, I took out the WHERE clause and it worked. Here is the new working code below:
MY_Controller
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
// $data object used for $this->data
public $data;
public function __construct() {
parent::__construct();
/*
* The $data array('KEY' => 'VALUE') variable sets are used
* in other Controller Classes that extend MY_Controller.
*/
$this->data = array(
'app_title' => $this->settings_model->get_title('$title'),
'meta_author' => APP_AUTHOR
);
}
}
Then in the Settings_model I made the following changes, remove ->where('app-title', $title);. Like so:
defined('BASEPATH') OR exit('No direct script access allowed');
class Settings_model extends CI_Model
{
public $title = '';
public function __construct() {
$this->load->database();
}
public function get_title($title) {
$this->db->select('app_title');
$this->db->from->('settings');
// I removed the following where->(); and it now loads the dynamic data
// $this->db-where('app_title', $title);
return $this->db->get()->row('app_title');
}
}
Now everything works as planned. I knew it was something simple and stupid 9for lack of better words). Thank you to #Mirceac21 for your input.

Related

Called controller can't access anything

in codeigniter I have my main controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller
{
public function index()
{
$this->load->library('../controllers/forum');
$obj = new $this->forum();
$obj->test();
}
}
And the controller I'm trying to access:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Forum extends CI_Controller
{
function __construct()
{
echo "testing1";
$this->load->library('session');
parent::__construct();
$this->load->database();
$this->load->model('model_forum');
}
public function index(){
}
public function test(){
echo "testing2";
$this->data['forums'] = $this->model_forum->getForums();
$this->load->view('homepage', $this->data);
}
}
Everything is fine with my model_forum.php file, because it works if I put all the code in Main controller. But if I'm trying to access Forum controller, nothing works, only "testing1" echo goes through. Picture of error:
Anyone has any idea what I'm doing wrong? I'm new to PHP and codeigniter so I'm struggling a little bit. Thanks in advance.
You can't load a controller from a controller in CI - unless you use HMVC or something.
You should think about your architecture a bit. If you need to call a controller method from another controller, then you should probably abstract that code out to a helper or library and call it from both controllers.
UPDATE
After reading your question again, I realize that your end goal is not necessarily HMVC, but URI manipulation. Correct me if I'm wrong, but it seems like you're trying to accomplish URLs with the first section being the method name and leave out the controller name altogether.
If this is the case, you'd get a cleaner solution by getting creative with your routes.
For a really basic example, say you have two controllers, controller1 and controller2. Controller1 has a method method_1 - and controller2 has a method method_2.
You can set up routes like this:
$route['method_1'] = "controller1/method_1";
$route['method_2'] = "controller2/method_2";
Then, you can call method 1 with a URL like http://example.com/method_1 and method 2 with http://example.com/method_2.
Albeit, this is a hard-coded, very basic, example - but it could get you to where you need to be if all you need to do is remove the controller from the URL.
You could also go with remapping your controllers.
From the docs: "If your controller contains a function named _remap(), it will always get called regardless of what your URI contains.":
public function _remap($method)
{
if ($method == 'some_method')
{
$this->$method();
}
else
{
$this->default_method();
}
}

Library not working on CodeIgniter

So I tried to create a library in which I can get some "global" data from one of my data tables (site name, logo, favicon, etc).
This is my current library:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Settings {
//holds the CI instance
//var $ci;
protected $ci;
//the array the settings will be stored as
public $settings = array();
//
public function __construct()
{
//load CI instance
$this->ci =& get_instance();
//fire the method loading the data
$this->get_settings();
}
public function get_settings()
{
$this->ci->load->model('settings_model');
$this->settings = $this->ci->Settings_model->get_list();
return $this->settings;
}
}
?>
This library should access to my method get_list() on my Settings_model and so far it seems to be able to do so (keep reading).
This is my settings model:
function __construct()
{
parent::__construct();
$this->table = 'ci_settings';
}
public function get_list()
{
$query = $this->db->get($this->table);
return $query->result();
}
Now to use the data which comes from the database in my views without having to declare them in all my controllers, I just have to do this(I did something similar with my menu library so I know it should work):
<?php echo $this->settings['website_name'] ?>
Here is the PROBLEM, my library is being able to access to my model which seems fine(I suppose), the problem comes when it tries to access to the get_list method itself; is apparently having some issues returning the data as this is the error that I get:
Should not this be possible just like this?.. what I'm trying is to have global variable for logo, website name, etc... I'm sure it should not be that hard but I'm having a really big pain in the *** trying to figure out a solution.
Any idea where is my error?,
Thanks in advance.
Try with ->settings_model-> with S in lowercase, but why ?
CI save the instance of your model internally like a property of the object CI that you have with function get_instance()
Remember: class properties are case sensitive

Codeigniter -- working with multiple controllers

I have a question which regards to controllers. Let's get it started:
I have a main controller named "admin.php", it has a menu for company, user management, etc. Each item on the menu has separate PHP file to hold different kinds data [I seems to be lengthy to combine them all in one php.
So for this example:
I have 3 controllers: admin.php , company.php, usermanagement.php
What I want is, link the company and management controllers as a child of admin. So if enter the address on the browser, it may look: localhost/admin/company and localhost/admin/usermanagement
I configured the routes and it's good but when I enter "localhost/company" it loads the company page which i didn't want to. i wanted to link them all as a child of an admin page.
How would I achieve this?
by the way here's a snippet of my code:
admin.php - Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends CI_Controller {
function __construct(){
parent::__construct();
session_start();
}
public function index() {
$this->load->view('view_admin');
}
}
Company - Same as the admin
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Company extends CI_Controller {
function __construct(){
parent::__construct();
session_start();
}
public function index() {
$this->load->view('view_company');
}
}
Thanks,
James
EDIT: I have tried adding functions on the admin.php like:
function company() {}
function usermanagement() {}
but I guess it wasn't that effective since it will include lots of functions later on as I try to migrate my native php codes into this MVC architecture framework.
If your issue is that you like the way routes work but don't want people to be able to visit index.php/company/ and rather prefer that they visit admin/company you can always do:
class Company extends CI_Controller {
public function __construct() {
parent::__construct();
if ( $this->uri->segment(1) != "admin" ) {
redirect('admin/company/'.$this->uri->segment(3));
}
}
...
Although bear in mind that you would probably need a more complete URL forming method than just adding $this->uri->segment(3) but the general pattern is there.
You can add more functions in your 'admin' controller, so, default page is:
public function index() {
$this->load->view('view_admin');
}
'Subpage' is:
public function company() {
$this->load->view('view_company');
}
etc, etc...

Add public variable to all CI_Controller's and anything that extends them?

Hi I'd like to add a public variable ($this->data[]) to all instances of CI_Controllers, that way I can store some base rules for outputting a page (css/js, etc) then have each controller append to this array to add its own requirements (more css/js). I have a core library with custom view functions that take those arrays and inject them into the head tag of the page template.
The options I've thought of;
Edit CI_Controller and add it there... guessing that's a bad idea.
Create a shell controller that extends CI_Controller, add the var to that, then have every other controller extend the shell controller.
Any other clever ways?
I've only been using CI for about a month and I've tried reading through the docs but I can't find any built in ways to do something like this? Has anyone encountered this before and if so how did you solve it?
Thanks!
[edit] Using PHP 5.3.x [/edit]
http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
You can extend CI_Controller to have the functionality you want.
application/core/MY_Controller.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function my_function()
{
return "Cool return from my_function";
}
}
controllers/welcome.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends MY_Controller {
public function index()
{
echo $this->my_function();//echo's "Cool return from my_function"
}
}
You simply define the functionality you want in MY_Controller. Then in your controllers, use extends MY_Controller instead of extends CI_Controller and you can call the functions anywhere inside those controllers.
I think what you want can be easily achieved using traits. Check here: PHP: Traits
More specifically -> Example #11 Defining Properties.
The only limitation is it's PHP 5.4+
You can create your own a base controller file to inherit basic page load methods from in \application\core\MY_Controller.php as such:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
}
protected function loadPage($path, $data = array())
{
$this->load->view('common/head'); // Assuming you will use a folder for page parts
$this->load->view($path, $data);
$this->load->view('common/foot');
}
}
Afterwards in your page controller you can call upon the same methods much more easily:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class User extends MY_Controller {
public function index()
{
$data = array(); // get data from model
$this->loadPage('user/view', $data); // Assuming you will use folders for sets of views
}
public function edit()
{
$data = array(); // get data from model
$this->loadPage('user/edit', $data);
}
}

codeigniter -> having trouble loading multiple libraries/classes

Ok, so in my base controller (page.php) I have the following code which works fine:
$this->load->library('Siteclass');
$mysite = new site_model();
The siteclass library references a model named site_model and instantiates based on data received from that model. All is good.
Now I want to load another library so that I can instantiate another object as well. So I add this to page.php:
$this->load->library('Memberclass');
$mysite = new member_model();
But now I get the following error:
Message: Undefined property: Memberclass::$site_model
Filename: libraries/Loader.php
Line Number: 1035
From what I can tell, it seems that the loader class, when being applied to the Memberclass, is somehow still referencing the site_model instead of the member_model. I've checked my code and I am definitely calling the correct files.
Here's what Siteclass.php looks like:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Siteclass extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Site_model');
$data = $this->Site_model->load_site_data();
// etc etc
and here's what Memberclass.php looks like:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Memberclass extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Member_model');
$data = $this->Member_model->load_member_data();
// etc etc
Thanks in advance for any help!
Gary
I think you're confused about how MVC works in CodeIgniter. Why are you using the loader class to create a controller? Why are you creating a stand-alone instance of your model outside of your controller class?
In CodeIgniter, your URLs represent paths to your controllers' methods. That means that your "base controller" should automatically be instantiated if you go to:
www.example.com/memberclass
Or perhaps more to the point, if you have a link like this:
www.example.com/page
You should have a file in your /application/controllers directory called page.php which looks like this:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Page extends Controller {
function __construct() {
parent::Controller();
// etc etc
Furthermore, unless you're loading data from your model to be used every single time you call this controller, you'll want to put your model calls inside a non-constructor method of this class. Something like:
class Page extends Controller {
function __construct() {
parent::Controller();
}
function index() {
$this->load->model('Member_model');
$data = $this->Member_model->load_member_data();
$this->load->view('myview', array('data'=>$data));
}
}
So again...not entirely sure what context you're doing this all in, but it seems like you're not standing firmly within the framework. There's basically no reason you should be using the loader class to load controllers, and furthermore there's no reason you should be creating stand-alone instances of model classes using PHP's new keyword.

Categories