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");
}
}
Related
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'm using Codeigniter 3 and I need some data available to all methods. I will query the data from the database and then I need to display it on every page.
I have created a MY_Controller extending CI_Controller and saved it in /application/core/
But I am getting this error:
Fatal error: Call to undefined method Area_model::get_user_locations()
MY_Controller:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public $location_data;
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('area_model');
$org_id = $this->session->userdata('org_id');
$this->location_data = $this->area_model->get_user_locations($org_id);
}
}
Can i access models and the database from within MY_controller?
Area_model.php
// get all areas for an organisation
public function get_user_locations($org_id) {
$areas = $this->db->get_where('areas', array('org_id' => $org_id));
$area_array = $areas->result_array();
return $area_array;
}
MY_Controller
not used to direct any function to __construct()
__construct to only load to any model
write other function to MY_Controller look like this
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public $location_data;
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('area_model');
$org_id = $this->session->userdata('org_id');
}
}
function functionname()
{
$this->location_data = $this->area_model->get_user_locations($org_id);
}
Area_model.php
public function get_user_locations($org_id)
{
$areas = $this->db->get_where('areas', array('org_id' => $org_id));
$area_array = $areas->result_array();
return $area_array;
}
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 have been working on a session validation for my login to make sure that a user is logged in to view pages. I keep getting this error:
Fatal error: Class 'MY_Staffcontroller' not found in /usr/local/var/www/CodeTest
/ci/application/controllers/staff_c.php on line 3
My staff_c page looks like so :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Staff_c extends MY_Staffcontroller {
function homepage()
{
$data['main_content'] = 'homepage_view';
$this->load->view('includes/template', $data);
}
}
I have been reading same questions all over the place and they say the same thing pretty much...
Is your controller located in application/core?
Well yes it is. I can't seem to get passed this hump!
This is the code within My_Staffcontroller.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_staffcontroller extends CI_Controller {
function __construct()
{
parent::__construct();
$loggedin = $this->session->userdata('loggedin');
if(!isset($loggedin) || $loggedin != TRUE);
{
die($this->load->view('denied'));
}
}
}
I know this is user error as this is only my second day with CodeIgniter but I can't seem to find proper workaround for this?
I have tried this tutorial and still nothing and also this
Even following this video has me stuck on the session part.
And I just can not get this to work.
Remember Linux is case-sensative whereas Windows is case-insensative.
place you're MY_Staffcontroller inside application/core/MY_Controller.php file
Your MY_Controller.php file should look like this (minus all you're other functions, this is a minimal example)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
}
class MY_Staffcontroller extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function sayHello()
{
echo "Hello, I am a function within MY_Staffcontroller.php";
}
}
Example
This will be located in /application/controllers directory
Basically any protected and public functions located in either MY_Controller OR MY_Staffcontroller will be accessible from derived controllers that extend the extended controller. In this case it would be MY_Staffcontroller
class Public_Staff_Controller extends MY_Staffcontroller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->sayHello();
}
}
/* end of file /application/core/MY_Controller.php */