I have a project whith this structure in controllers folder:
places.php
users.php
items.php
...
And in my models folder:
Place.php (the name inside is class Place extends ActiveRecord\Model)
User.php
...
Inside my controller places.php if I want to load a model I have to do just that:
$this->load->model('Place');
And after that I have to call my method like that:
$this->place->get_all_places();
It was work in my localhost but not in my server I check my php version in the server and it's 5.6.
How I can fix that?
That's my model file Place.php
class Place extends ActiveRecord\Model
{
static $table_name = 'places';
static $has_many = array(
);
public function get_all_places()
{
return true;
}
}
That's my controller file places.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Places extends MY_Controller {
function __construct()
{
parent::__construct();
if($this->user){
$access = TRUE;
}else{
redirect('login');
}
}
function index()
{
$this->content_view = 'places/all';
}
function get_places(){
$this->theme_view = '';
$this->load->model('Place');
$this->place->get_all_places();
}
}
And the error it was that:
Unable to locate the model you have specified: place
Your model file name should be
Places_model.php
and inside model
class Places_Model extends CI_Model # Changed
{
static $table_name = 'places';
static $has_many = array();
public function get_all_places()
{
return true;
}
}
Models in Codeigniter
I think every thing is right.But the problem is due to this line..
class Place extends ActiveRecord\Model
If you define a new model which extends the CI_Model.And then name of new model can not like as ActiveRecord\Model.Name may be as ActiveRecordModel.SO one solution for you can be..replace above line as below if you define the new model named as ActiveRecordModel in application/core folder.
class Place extends ActiveRecordModel
Another solution can be if you have not define any new model.then just replace above line as
class Place extends CI_Model
Related
I am new to Laravel. I have extended a common controller by other controllers in Codeigniter. Here is my CI common controller.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Common extends CI_Controller {
protected $_data = array();
public function __construct() {
parent::__construct();
$this->load->helper(array('url','form','html'));
$this->load->library(array('session','authentication','upload','image_lib','pagination'));
$this->_data['totalUser'] = 10;
$this->_data['newUser'] = 2;
$this->_data['totalChallenge'] = 1;
$this->_data['totalReport'] = 1;
}
}
This is how I extended it in other CI controllers:-
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include_once('common.php');
class Dashboard extends Common {
public function __construct() {
parent::__construct();
$this->load->model('login_model');
}
public function index()
{
$data['globalData'] = $this->_data;
$entity = 'dashboard';
$action = 'view';
$data['action'] = $action;
$data['entity'] = $entity;
$this->authentication->is_loggedin($this->session->userdata('user_name'));
$this->load->view('admin/dashboard',$data);
}
}
Now, in view page, I can just print the values like <?php echo $globalData['totalUser'];?>
Why am I doing this?
Suppose, today I need to add 20 more data in all the controllers. Then I will have to change only in the common controller. Since $data['globalData'] = $this->_data; will make all 20 data available in every controller, it is easy for coder to write one LOC instead of 20 LOC.
How can I do the above work in Laravel 5.2?
In Laravel you have a base controller located in App\Controller.
You can use it exactly as are you using your CIController, because all the generated controllers (by Artisan) are extending that base project controller.
You can easily create the base controller which extents Laravel Controller in same directory ie(/controllers)
Following sample
DashboardController.php
class DashboardController extends \BaseController {
public function index()
{
$data['globalData'] = $this->_data;
$entity = 'dashboard';
......
}
BaseController.php (extends laravel Controller)
<?php
class BaseController extends Controller {
/**
*
* define your value
*/
protected $_data = "20";
}
please add if anything is missing
I have setup the codeigniter, default controller is working(welcome.php) but when i add new controller its not working
Default controller :
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
public function testing()
{
$this->load->view('test');
}
}
http://localhost/appointment/index.php/welcome/ (working)
New controller
class democontroller extends CI_Controller {
public function index()
{
$this->load->view('test');
}
}
http://localhost/appointment/index.php/democontroller/ (not working)
Change the following code:
class democontroller extends CI_Controller {
}
to
class Democontroller extends CI_Controller {
}
and save this file with name Democontroller.php and try again.
Note: controller name first character must be capital as per naming convention.
I will extend the controller based of login typical, so I create on application directory like this:
core
-MY_Controller
-public controller
MY_Controller
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
}
public controller
<?php
class Public_Controller extends MY_Controller
{
//Some Logic here
public $layout = 'layout';
}
Now, time to use those things.
I write on application/route $route['default_controller'] = 'Home';
So, the controll that named Home would be like this :
<?php
class Home extends Public_Controller {
public function index() {
$this->load->view('public/home');
}
}
But unfortunately, it gives me error like this :
Fatal error: Class 'Public_Controller' not found in C:\wamp\www\egi\application\controllers\Home.php on line 5
Why I can not to extends the sub class ?
NOTE
But if I extends from MY_Controller, its success
<?php
class Home extends MY_Controller {
public function index() {
$this->load->view('public/home');
}
}
Any help it so appreciated
You can do this in one of the following ways.
Have a file named Public_controller in "application/core" & include that file in your application/core/MY_Controller.php file.
Define Public_Controller inside MY_Controller file which is autoloaded by default which means you'd end up with something like this.
File: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
public function __construct () {
parent::__construct();
}
}
class Public_Controller extends CI_Controller {
public function __construct () {
parent::__construct();
}
}
I am having trouble loading model in sub module controller from sub module itself.
I am having few modules and sub-modules as below
modules/admin/
modules/admin/models
modules/admin/controllers
modules/admin/views
modules/admin/models/dashboard/
modules/admin/controllers/dashboard/
modules/admin/views/dashboard/
modules/admin/models/plugs/
modules/admin/controllers/plugs/
modules/admin/views/plugs/
Each M/V/C has own files in it.
Now I have created model in modules/admin/models/plugs/ just for testing purpose something like below
Plugs Model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Plugs extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function mymeta()
{
return 'plugs model loded';
}
}
And now trying to load into Plugs Controller as below
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
class Plugs extends MX_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('plugs');
}
public function index()
{
$this->load->view('plugs/index');
}
public function get_plugin_meta()
{
echo $this->plugs->mymeta(); // this is the method from Plugs Models
}
}
But when I tried to access the URL http://localhost/mysite/admin/plugs/get_plugin_meta or http://localhost/mysite/admin/plugs it is giving me below error.
An Error Was Encountered
Unable to locate the model you have specified: plugs
So than how to load model in controller?
Your module is not plugs. It's admin because of modules/admin
You should use like $this->load->model('module/model');
Try this one:
$this->load->model('admin/plugs/plugs'); // module/folder/file
Important
Controller class name and model class name was conflicting.
Just rename model filename with: plugs_model.php
and change class name with: class Plugs_model extends CI_Model
$this->load->model('admin/plugs/plugs_model');
echo $this->plugs_model->mymeta(); // plugs model loded
I've recently switched from Windows 7 to Ubuntu.. Now I'm having this strange problem with codeigniter.. My code works prefect on Widnows 7 Xampp server, but when I try to access it on ubuntu having apache2, I cannot load any model, libraries etc.
Here is my code for model
<?php
class Usermodel extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->user_per_page = 8;
}
function getUser($id)
{
$query = $this->db->query("SELECT * FROM user WHERE id = $id");
if(intval($query->num_rows()) > 0)
{
$data = $query->result();
return $data[0];
}
else
return null;
}
}
Here is the code of my controller where i am loading model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller
{
public function index()
{
$this->load->model("Usermodel");
$fb_config = array(
'appId' => 'xxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxx'
);
$this->load->library('facebook', $fb_config);
$this->load->helper('security');
}
}
I've already tried the following
1. Changed usermodel.php to Usermodel.php
2. Changed $this-load->model("Usermodel") to $this-load->model("usermodel")
but none seem to work
I get this fatal error when i call $this->Usermodel->getUser(1) in index() function of my controller
PHP Fatal error: Call to a member function getUser() on a non-object in /var/www/voicebuds/application/controllers/register.php on line 19, referer: mysite
UPDATE
If i put the Usermodel in config/autoload.php, it works fine.. So I must say there is some problem with loader function.
check your model file name for model
<?php
// file name usermodel.php
class Usermodel extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->user_per_page = 8;
}
function getUser($id)
{
$query = $this->db->query("SELECT * FROM user WHERE id = $id");
if(intval($query->num_rows()) > 0)
{
$data = $query->result();
return $data[0];
}
else
return null;
}
}
in your controller load appropriate model using model file name
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller
{
public function index()
{
$this->load->model("usermodel");//your file name of model
$fb_config = array(
'appId' => '454005957970086',
'secret' => '7b8e9664cb0439f88da00007996278c7'
);
$this->load->library('facebook', $fb_config);
$this->load->helper('security');
}
}
Model classes are stored in your
application/models/
folder. They can be nested within sub-folders if you want this type of organization.
The basic prototype for a model class is this:
class Model_name extends CI_Model {
function __construct()
{
parent::__construct();
}
}
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.
The file name will be a lower case version of your class name. For example, if your class is this:
class User_model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
Your file will be this:
application/models/user_model.php
So please check, is your model in any sub-folder or not.
It is said to be a best practice to load models, libraries in __construct (constructor)
class User extends CI_Controller
{
public function __construct()
{
parent:: __construct();
$this->load->model('usermodel');
}
}
I found in your controller there is not construct. try to add constructor and load usermodel there
I've ended up using __construct() in controller class.. I now first initialize model in constructor and then call in in function and its working now.!! But still I'm puzzled why load-model() didn't work for me in index() function
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller
{
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->load->model("Usermodel");
$fb_config = array(
'appId' => 'XXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXX'
);
$this->load->library('facebook', $fb_config);
$this->load->helper('security');
}
public function index()
{
$data = $this->Usermodel->getUser(1);
print_r($data);die;
}
}