I am having error in my code, this is not working properly can anyone tell me where is the problem?
<?php
class Blog extends CI_controller {
public function jay()
{
$this->load->model('abc');
$this->load->getdata();
print_r($data);
$this->load->view("page1.php");
}
/*public function wel()
{
$this->load->view('Welcome_message');
} */
}
?>
If the method getdata(); is inside your model you can not access it by using
$this->load function.
you can access it by $this->abc->getdata();
Related
contrller:News.php
This is my controller News
<?php class News extends CI_Controller {
public function __construct()
{
}
public function getShowIN_News()
{
return $result;
} } ?>
contrller:Category.php
This is my controller Category
<?php class Category extends CI_Controller {
public function __construct()
{
}
public function category()
{
require('news.php');
$test = new News();
$data["headlines"] = $test->getShowIN_News();
} }?>
By using an empty constructor, you're making it so that CI_Controller::__construct() isn't called, and that's where everything in the framework is initialized.
I know you've put it there to hack it so you can call one controller from another, but it is very intentionally made that way, exactly so you don't do this.
I have a controller:
class Blah extends Controller
{
function Blah()
{
$this->load->model('baga_model');
}
}
then comes baga_model:
class Baga_model extends Model
{
function do_it()
{
echo "BOOM!";
}
}
..and
class Blah_model extends Model
{
function some_action()
{
$this->baga_model->do_it();
}
}
So .. when in blah_model I call $this->baga_model->do_it() I get an error :
Call to a member function do_it() on a non-object
I just can't understand why.... I know it must work, I did something similar before..
Thanks
Got it! I had to load baga_model in blah_model constructor. This way it works.
Thanks everyone.
public function test()
{
$this->load->model('baga_model');
$this->baga_model->do_it();
}
Model
class baga_model extends CI_Model
{
public function do_it()
{
echo $this->bar("BOOM!");
}
Your not loading your required model inside your model:
class Blah_model extends CI_Model
{
$this->baga_model = $this->load->model('baga_model', true);
public function some_action()
{
$this->baga_model->do_it();
}
}
i am trying to use $admin->function() instead $this->admin_model->function()
when i tried to declare a variable $admin=new Admin_model; in constructor and use it in other functions it gives error..
my code is given below, i don't know much about OOP concept, somebody please help.
class Admin extends CI_Controller {
var $admin;
function __construct() {
parent::__construct();
$this->load->model('admin_model');
$admin=new Admin_model;
}
public function index($value='')
{
if(!$admin->is_admin_logged_in()){
redirect('admin/login?r='.urlencode(current_url()));
}
$data['loggedin']=TRUE;
$data['account']=$this->session->all_userdata();
$this->load->view('pages/admin-home',isset($data)?$data:NULL);
}
}
presently i am using this method
public function login()// this function belongs to the same controller mentioned above
{
$r=isset($_GET['r'])?urldecode($_GET['r']):'admin';
$admin=new Admin_model;
if($admin->is_admin_logged_in()) redirect($r);
}
i don't want declare $admin=new Admin_model; in every single function and want to make the code look good and clean, so don't like to use $this->admin or $this->admin_model either.
You are trying to give an alias to a model (as what I can see)
All you have to do is:
class Admin extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('admin_model', 'admin');
}
public function index($value='')
{
if(!$this->admin->is_admin_logged_in()){
redirect('admin/login?r='.urlencode(current_url()));
}
$data['loggedin']=TRUE;
$data['account']=$this->session->all_userdata();
$this->load->view('pages/admin-home',isset($data)?$data:NULL);
}
}
Notice where the alias of the model is given
$this->load->model('admin_model', 'admin');
And how it is used
if(!$this->admin->is_admin_logged_in()){
I'm new on Laravel 4 and I am trying to understand it.
is searched on google and on stackoverflow. Maybe i am not searching for the right syntax but i hope someone can help me, with it.
In CodeIgniter i understand it (probably). There I use in an Controller:
function __construct()
{ $this->load->model('example_m'); }
But how about in Laravel 4?
I figured out the following:
i make a static function in de model so i can access it everywhere. Example:
class Example extends Eloquent // this is the model
{
public static function TestExample(){
// do some stuff here
}
}
Or i could do it like this:
class ExampleController extends BaseController
{
public $test = null;
public function __construct()
{
$this->test = new Example();
}
public function index()
{
$this->test->TestExample();
}
}
My question is: Is there an other way and/or what is the correct way?
http://four.laravel.com/docs/ioc
App::bind('ExampleModelInterface', 'Example');
class ExampleController extends BaseController {
public function __construct(ExampleModelInterface $model)
{
$this->model = $model;
}
}
Do you mean simply accessing the method of a model?
Since they are static you use: Modell::method()
You might have to do a composer dump-autoload though so L4 autoloads it correctly.
I'm sure this is a simple mistake on my part. I'm trying to demo ci to a friend and we created a simple controller that looks like this:
<?php
class Customerlookup extends CI_Controller {
//constructor for this class
public function __construct()
{
parent::__construct();
$this->load->model('customerlookup_model');
}
public function index()
{
echo 'test';
}
public function loadcustomers()
{
$data['cust'] = this->customerlookup_model->get_customers();
$this->load->view('customerlookup',$data);
}
}
And here's what the model looks like:
<?php
class Customerlookup_model extends CI_Model
{
public function __construct()
{
parent::Model();
$this->load->database();
}
public function get_customers()
{
$query = $this->db->get('customer');
return $query->result_array();
}
}
If I try to test this out by doing either:
localhost/myapp/index.php/customerlookup/loadcustomers
or
localhost/myapp/index.php/customerlookup/
nothing happens and no errors appear and no data or messages either. We are using the latest CodeIgniter (2.1.3).
Any suggestion?
i found the problem. php errors were not turned on. there were some syntax problems. checked the apache error log and figured out that the errors were just not being displayed
thanks for the help guys
you are missing with your url helper
$this->load->helper('url');
into your
class Customerlookup extends CI_Controller {
//constructor for this class
public function __construct()
{
parent::__construct();
$this->load->model('customerlookup_model');
$this->load->helper('url');
}
public function index()
{
echo 'test';
}
public function loadcustomers()
{
$data['cust'] = this->customerlookup_model->get_customers();
$this->load->view('customerlookup',$data);
}
}