I have set up a project with CodeIgniter 2.1 and Doctrine 2.2, following the instruction on
Doctrine cookbook. The EntityManager works, but when I try to load entity models, it
gives me error
Fatal error: Class 'Users' not found in /Volumes/Data/Projects/myproject/application/controllers/home.php on line 10
This is my home.php file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//require_once(APPPATH.'models/Users.php');
class Home extends CI_Controller {
public function index()
{
$em = $this->doctrine->em;
$users = new Users;
//$user = $em->find("Users", 1);
$em->flush(); // dummy
$this->load->view('welcome_message');
}
}
If I uncomment line 3: require_once(APPPATH.'models/Users.php');, then
it works perfectly.
How can I make the models auto-loaded?
Is the autoload mechanism handled by the bootstrap in libraries/ Doctrine.php, isn't it?
$entitiesClassLoader = new ClassLoader('models', rtrim(APPPATH, "/" ));
$entitiesClassLoader->register();
Please anyone give me insight about this issue.
I'm guessing that you have a namespace line inside of your Users.php file?
If so then you will need to add a "use" statement to your controller code so the php namespace system knows where to look for your entities.
If not then verify that your entitiesClassLoader is actually being called and that APPPATH has the expected value.
I've never worked with CI and Doctrine so don't know if Doctrine should load the models or where it loads them, but you should try to do it yourselft, not with an include but this way :
In the Controller :
$this->load->model('Users');
Or as Autoload in application/config/autoload.php and load the model in the Array.
Related
First of all, I am having very little knowledge of CodeIgniter 3 and now I am first time using the new CodeIgniter 4. For the HMVC module building, I was using MX_Controller in CI3. Now when I came to know the CI4 is by default supporting HMVC I am trying to make use of it. When I try to port my first module I am getting an error in the Controller about the constructor.
How can I load my model in this controller?
Can't I load my model in the constructor straight away?
Template.php [Controller]
<?php
namespace Modules\Template\Controllers; // sPiDeR adder namespace for CI4 support
//defined('BASEPATH') OR exit('No direct script access allowed');
class Template extends \CodeIgniter\Controller { // Using CI controller instead of MX Controller
public function __construct()
{
parent::__construct();
$this->load->model(array(
'template_model'
));
}
public function layout($data)
{
$id = $this->session->userdata('id');
$data['notifications'] = $this->template_model->notifications($id);
$data['quick_messages'] = $this->template_model->messages($id);
$data['setting'] = $this->template_model->setting();
$this->load->view('layout', $data);
//echo view('layout', $data); //sPiDeR update syntax change
}
public function login($data)
{
$data['setting'] = $this->template_model->setting();
$this->load->view('login', $data);
//echo view('login', $data); //sPiDeR update syntax change
}
}
Error I am getting in Debugger
Please use model like that its working surely.
use App\Models\Model1;
use App\Models\Model2;
class Myclass extends Backendcontroller
{
public function __construct()
{
$this->Model1 = new Model1();
$this->Model2 = new Model2();
}
$foo = $this->Model3->where('bar', $bar)->findAll();
I'll recommend using Upgrading from 3.x to 4.x guide while you migrate your app from CI3 to CI4. All the changes you might require to convert your app from version 3 to 4 are explained in it.
Steps for upgrading models include what you need. Quoting only part relevant to question:
Instead of CI3’s $this->load->model(x);, you would now use $this->x = new X();, following namespaced conventions for your component. Alternatively, you can use the model function: $this->x = model('X');.
You can load models from within the constructor if you follow one of these approaches.
I want to add some code that will run for every controller. Adding the code to CodeIgniter's CI_CONTROLLER class seems unconventional.
Where is the right place to include code you want to run for every controller?
Here is the code:
require_once 'vendor/autoload.php';
$bugsnag = Bugsnag\Client::make("my-secret-key-is-here");
Bugsnag\Handler::register($bugsnag);
These classes both come from a dependency installed with Composer.
I suspect I should create a helper, and include it in application/config/autoload.php. But new to CodeIgniter, so not sure of conventions.
Edit: I am using CodeIgniter 3.1.6.
If you want to execute arbitrary code at different points in CodeIgniter's life cycle, you can use the hooks feature.
Official Documentation:
https://codeigniter.com/user_guide/general/hooks.html
1. Enable hooks
Go to /application/config/config.php.
Search for enable_hooks and set it to true: $config['enable_hooks'] = TRUE;
2. Add desired code to CodeIgniter's hook file:
Go to /application/config/hooks.php.
Choose the desired lifecycle to hook into (see doc link above for a list)
Add code to the lifecycle, e.g. $hook['pre_controller'] = function(){... your code goes here ...}
For this question's example, my hooks.php looks like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// This is the code I added:
$hook['pre_system'] = function(){
require_once 'vendor/autoload.php';
$bugsnag = Bugsnag\Client::make("my-client-key");
Bugsnag\Handler::register($bugsnag);
}
?>
I would just extend the Controller Class.
See "Extending Core Class":
"If all you need to do is add some functionality to an existing library - perhaps add a method or two - then it’s overkill to replace the entire library with your version. In this case it’s better to simply extend the class."
...
"Tip: Any functions in your class that are named identically to the methods in the parent class will be used instead of the native ones (this is known as “method overriding”). This allows you to substantially alter the CodeIgniter core."
class MY_Controller extends CI_Controller {
....
}
Any function you put inside would be added to the core, otherwise, if you use the same name as an existing method, it would replace just that one method.
You'd name it MY_Controller.php and put it inside application/core/, where it's picked up to override CI_Controller automatically.
If you are extending the Controller core class, then be sure to extend your new class in your application controller’s constructors.
class Welcome extends MY_Controller {
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function index()
{
$this->load->view('welcome_message');
}
}
Looks like you could also use a pre_system or pre_controller hook as described here:
https://www.codeigniter.com/user_guide/general/hooks.html
Assuming it's CodeIgnitor 3.X
Go to application/config/config.php and change:
$config['composer_autoload'] = FALSE;
to
$config['composer_autoload'] = TRUE;
just below the above line include
require_once APPPATH.'vendor/autoload.php';
Or in your controller include
require_once APPPATH.'vendor/autoload.php';
From How to use composer packages in codeigniter?
Go to application/config/config.php and set $config['composer_autoload'] = FALSE; to TRUE:
Enabling this setting will tell CodeIgniter to look for a Composer
package auto-loader script in application/vendor/autoload.php.
As a result you need not to call require_once 'vendor/autoload.php';.
I am using codeigniter 3
in application/config/config.php file I have added this autoload code for model
function __autoload($class) {
if (file_exists(APPPATH."models/".strtolower($class).EXT)) {
include_once(APPPATH."models/".strtolower($class).EXT);
}
}
to autoload model
and I am using model in controller like this
public function index()
{
$post = new post();
}
but it is showing error
Class 'post' not found
I do have post model in model folder already created
I am using the autoload code from source
http://code.tutsplus.com/tutorials/6-codeigniter-hacks-for-the-masters--net-8308
but it is not working like shown in blog.
Do I need anything else to update more for this?
If you need to autoload a model in your CI3 app, just go in application/config/autoload.php and find the line :
$autoload['model'] = array();
Then, add the model you want to autoload :
$autoload['model'] = array('my_model', 'my_second_model');
Then in your controller, you don't need to create a new instance of your model class. Example :
$res = $this->my_model->myfunction();
Use capital letter for your class name.
Btw. I agree with first answer.
I have installed cakephp in document root folder and renamed it to todo. So the complete path is (C:\localhost\todo). I am able to run the index.php perfectly fine (all tabs are green).
I created the 'todo' example application from the book 'Beginning CAKE PHP - Novoice to Professional'. I keep getting the error 'MISSING CONTROLLER' even though I have the items_controller.php file. I am thinking for some reason the application does not know where the controller file is present.
The complete error is :
Missing Controller Error: ItemsController could not be found. Error:
Create the class ItemsController below in file:
app\Controller\ItemsController.php
<?php
class ItemsController extends AppController {
}
I have .htacess and index.php files in respective folder
Can someone please help.
Controller class names are plural, CamelCased, and end in Controller. So your controller name should be ItemsController.php not items_controller.php. See here for more info on Controllers.
class ItemsController extends AppController {
//class code here
}
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.