I am in the process of upgrading CI from 1.x to 3.x
From my application/controller/home.php which has a class:
class Home extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('app_controller');
}
...
And inside application/libraries/App_Controller.php
class App_Controller extends CI_Controller {
function __construct() {
parent::__construct();
$config_app = $this->config->load('Application');
...
From application/config/Application.php I am defining variables:
$config['env'] = 'dev';
$config['https'] = '//'.$_SERVER['HTTP_HOST'].'/';
$config['js'] = '/ui/js/';
But this returns an error
An uncaught Exception was encountered
Type: Error
Message: Class 'CI_App_controller' not found
Filename: C:\xampp\htdocs\myproject\system\core\Common.php
Any suggestions how to overcome this error and why App_Controller is not found.
Thank you!
your libraries shouldn't extend to CI_controller.
What you want to do in that case is move your App_controller into application/core/App_controller.php.
Then go to your application/config/config.php and change your $config['subclass_prefix'] = 'MY_'; to $config['subclass_prefix'] = 'App_';
Finally your Home controller should extend to App_controller instead like so:
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Home extends App_Controller {
function __construct() {
parent::__construct();
$this->load->library('app_controller');
}
}
Related
My Project Structure is
application
controllers/site
core/
views/site
I want when i entered http://mywebsite admin has to be loaded and when i entered http://mywebsite/site frontend has to be loaded
I went through some tutorials and i have done changes below
In config/routes.php
$route['default_controller'] = 'admin';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['site'] = 'site/home';
In core/My_Controller.php
defined('BASEPATH') or exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
}
class Admin_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
}
}
class Site_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
}
}
In controller/site/Home.php
defined('BASEPATH') or exit('No direct script access allowed');
class Home extends Site_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view("site/index.php");
}
}
But I am getting 404 when I opened http://mywebsite/site
Please help me
$route['site'] = 'site/home';
What that line does is that it defines a route that looks for a method called home inside the controller called Site viz. Site_Controller.
So you are getting the 404 error because your Site_Controller does not have the home method.
Try changing the Site_Controller like so...
class Site_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
}
# added this method
public function home()
{
$this->load->view("site/index.php");
}
}
Well i have defined a derived class from CI_Controller known as application that is present in the system/core/folder
<?php
Class Application extends CI_Controller {
function __construct() {
parent::__construct();
}
}
?>
and derived an class from application named as home which displays the home page
<?php
Class Home extends Application {
function __construct() {
parent::__construct();
}
public function index() {
$this->load->view("Home");
}
}
?>
But i still get an error saying that Class 'Application' is not found
Change core class name from 'Application' to 'MY_Controller'(class name must ends with _Controller) and save it as MY_Controller.php
Class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
}
}
Then extend the application controller class
Class Home extends MY_Controller {
You can use any words instead of MY_. For this change the following line in config.php
$config['subclass_prefix'] = 'MY_';
Make sure only Uppercase letters are allowed.
I am using PHP MVC CI. BaseController and LoginController are both inside Controller Folder. When I run the Login Controller. It says..
Class 'BaseController' not found
Login Controller
<?php
if ( ! defined('BASEPATH')) die('No direct script access allowed');
class loginController extends BaseController {
function __construct() {
parent::__construct();
}
public function login() {
$this->load->View("template/header");
$this->load->View("login_view");
}
}
?>
Base Controller
<?php
class BaseController extends CI_Controller {
function __construct()
{
session_start();
$this->load->library('session');
parent::__construct();
}
}
?>
Any idea why this is happening ?
If you need a parent controller than should be in
Path - application/core/MY_Controller.php
http://www.codeigniter.com/user_guide/general/core_classes.html
<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
}
If you also autoload sessions then you will not need to use session_start.
http://www.codeigniter.com/user_guide/libraries/sessions.html
Filename: Welcome.php must be first letter uppercase in CI3
<?php
class Welcome extends MY_Controller {
}
CI 3 is case sensitive
Both user guides now here. CI2 & CI3 http://www.codeigniter.com/docs
You just need to change the file path , as follows:
application > controllers > Login.php
<?php
if ( ! defined('BASEPATH')) die('No direct script access allowed');
class loginController extends BaseController {
function __construct() {
parent::__construct();
}
public function login() {
$this->load->View("template/header");
$this->load->View("login_view");
}
}
?>
And,
application > core > Base_controller.php
<?php
class BaseController extends CI_Controller {
function __construct()
{
session_start();
$this->load->library('session');
parent::__construct();
}
}
?>
Codeigniter wont load it from the same folder.
You either put both classes in the same file, not desirable or put the base controller in application/core.
You will need to also set your prefix for you extended controller, for example BASE_Controller.
application/config/config.php
$config['subclass_prefix'] = 'BASE_';
Codeigniter has good docs, and what you are after can be found here
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'm using CodeIgniter (v1.7.2) and I've created a custom controller that incorporates authentication called MY_Controller (based on David Winter's blog post). When I load any controllers that use this base class, I get this error;
*Message: Undefined property: MY_Controller::$session*
Note that I am autoloading 'session' (and 'MY_controller' as a library) like so:
$autoload['libraries'] = array('database', 'session', 'MY_Controller');
Here is MY_Controller:
class MY_Controller extends Controller {
public function __construct() {
parent::__construct();
if (!$this->session->userdata('loggedin')) { <-- error is here
header('Location: /sessions/login');
exit();
}
}
}
Here's the controller that I'm trying to load:
class Welcome extends MY_Controller {
function __construct() {
parent::__construct();
}
function index() {
$this->load->view('header');
$this->load->view('welcome_message');
$this->load->view('footer');
}
}
When I var_dump $this->session above where the error occurs, I can see that it is NULL. Even putting $this->load->library('session'); in MY_Controller's constructor doesn't work. Why isn't it loading properly?
Thanks
Try taking MY_Controller out of the autoload.
$autoload['libraries'] = array('database', 'session');
You are extending the controller class which is automatically loaded by codeigniter as it is part of the core