the default of CI of loading views is:
$this->load->view('path');
but what if i wanted to do something like
$this->load->adminView('path')
then i can prefix the path in adminView followed by the path
how would i do this?
thanks
go to ../System/Core/Loader.php, line 417 -> 210 (CI 2.10)
public function view($view, $vars = array(), $return = FALSE)
{
return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
}
change your function name (and maybe some code else) as you wish, be careful!
In application/core/ make a new Controller:
<?php
if(!defined('BASEPATH'))
exit('No direct script access allowed');
class Admin_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function load_admin_view($path, $data = '', $return = false)
{
return $this->load->view("admin_dir/" . $path, $data, $return);
}
}
?>
Then make your current controller extend this controller:
class Page extends Admin_Controller
Instead of
class Page extends CI_Controller
Then you can use:
$this->load_admin_view("path");
Related
I was uploading my code based on CI to a live website and it keeps getting error 404.
The web structure is like this :
web
-Application
-Assets
-Cache
-..so on
index.php
Here's my Application/Config/Routes.php file:
$route['default_controller'] = 'H';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
This is my H controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class H extends CI_Controller {
function __construct(){
parent::__construct();
if(empty($this->session->userdata("userData")['id'])) redirect('/login');
}
public function index()
{
$data = array();
if(isset($_GET['guid'])){
$data['post'] = getListPost($_GET['guid']);
}else{
$data['post'] = getStickyPost();
}
$this->load->view('partials/header');
$this->load->view('home',$data);
$this->load->view('partials/sidebar-home');
$this->load->view('partials/footer');
}
public function materi()
{
$this->load->view('partials/header');
$this->load->view('management/post/lists');
$this->load->view('partials/footer');
}
}
And this is my Login Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function index($slug="")
{
if(isset($_POST['username'])){
$cek = $this->checkLogin($_POST['username'], $_POST['password']);
if($cek){
$newdata = array("userData"=>array(
'name' => $cek[0]['nama'],
'id' => $cek[0]['id'],
'nip' => $cek[0]['nip'],
'jabatan' => $cek[0]['jabatan'],
'unit_kerja' => $cek[0]['unit_kerja'],
'foto' => $cek[0]['foto'],
'hak_akses' => ($cek[0]['hak_akses_knowledge_management']=="")?"pengguna":$cek[0]['hak_akses_knowledge_management'],
'unit_kerja_atasan' => $cek[0]['unit_kerja_atasan'],
'email' => $_POST['username'],
'logged_in' => TRUE));
$this->session->set_userdata($newdata);
redirect('/');
}else{
$this->session->set_flashdata('login_status','false');
redirect('/login');
}
}
// $this->load->view('partials/header');
// $this->load->view('partials/login');
// $this->load->view('partials/footer');
$this->load->view('partials/single_login');
}
public function logout(){
$this->session->sess_destroy();
redirect('/');
}
public function newlogin(){
$this->load->view('partials/single_login');
}
function checkLogin($email,$password){
$this->db->where("nip",$email);
$this->db->where("password",$password);
$query = $this->db->get("data_pegawai");
return $query->result_array();
}
}
When I change the routes to default it works, somehow when i return to 'H' it keeps getting error 404.
This is the structure of views folder :
management
infografis
post
ebook
partials
header.php
single_login.php
footer.php
sidebar-home.php
login.php
category
infografis.php
ebook.php
errors
home.php
post.php
welcome_message.php
Routes are expected to be lowecase, even if Controller file names and class names begin with an uppercase character.
So, H.php, class H extends CI_Controller are correctly capitalized. However, $route['default_controller'] should be assigned a lowercase value.
Try using
$route['default_controller'] = 'h'; instead of 'H' and you should be up and running
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Employee extends CI_Controller {
public function _construct()
{
parent::_construct();
$this->load->model('employee_model');
$this->load->helper(array('form','url'));
}
public function index()
{
$this->load->view('employee_form');
}
public function employee_form()
{
$save=array(
'emp_name' => $this->input->post('emp_name'),
'emp_gender' => $this->input->post('emp_gender'),
'emp_email' => $this->input->post('emp_email'),
'emp_phone' => $this->input->post('emp_phone'),
'emp_address' => $this->input->post('emp_address')
);
$this->employee_model->saveemployee($save);
redirect('employee/index');
}
}
This is my code above and error shown blow I am fresher in CodeIgniter I need help
This is an error
Alway model name is start from capital letter. First fixd that. Then chek database setting
You have to load the db library first. In
autoload.php :
$autoload[‘libraries’] = array(‘database’);
public function _construct()
{
parent::_construct();
$this->load->model('employee_model');
$this->load->helper(array('form','url'));
private $employee_model = 'employee_model';
}
First set it in construct
and then, call it like this
$this->{employee_model}->saveemployee($save);
I'am trying call custom library function witch should return inputed values back to form, but I'am geting error.
enter image description here
Controler:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Guest extends CI_Controller {
public function __construct(){
parent::__construct();
error_reporting(E_ALL ^ (E_NOTICE));
$this->load->helper(array('form', 'url', 'date'));
$this->load->library('form_validation', 'uri');
$this->load->model('dayData');
$this->load->library('session');
$this->load->library('guest');
}
public function index(){
$this->login();
}
public function dataToView($data, $viewFile){
$this->load->view('template/header');
if($viewFile != ''){
$this->load->view('user/' . $viewFile, $data);
}
$this->load->view('template/footer');
}
public function login(){
$this->dataToView($data, 'guest/login');
}
public function registration(){
if($this->input->post('registrationSubmit') !== null) {
//$this->form_validation->set_error_delimiters('<div class="alert alert-warning" role="alert">', '</div>');
$this->config->load('form_validation');
$this->form_validation->set_rules($this->config->item('registrationValidation'));
if($this->form_validation->run() == FALSE){
var_dump($this->guest->registrationPost());
$this->dataToView($data, 'guest/registration');
} else {
echo "string";
}
} else {
$this->dataToView($data, 'guest/registration');
}
}
}
Library:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Guest {
protected $CI;
public function __construct(){
// Assign the CodeIgniter super-object
$this->CI =& get_instance();
//$this->CI->load->model('Guest');
$this->CI->lang->load('error', 'english');
}
public function registrationPost(){
$result = array('name' => $this->CI->input->post('name'),
'nickName' => $this->CI->input->post('nickName'),
'email' => $this->CI->input->post('email'));
return $result;
}
}
There is a naming conflict between the controller and the custom class. They should not have the same name.
You have duplicate class names. The controller class name is Guest and the library class name is also Guest.
When CodeIgniter goes to load the Guest library, it will skip over it and log a debug message. https://github.com/bcit-ci/CodeIgniter/blob/develop/system/core/Loader.php#L1032
I suggest renaming your library to something else, Registration perhaps?
I am not sure whats wrong with my code.......it causing an error while loading model.......
please help...........
my controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Exams extends CI_Controller {
public function index(){
$ob = new exam_class();
$ob->set_exam(0,'Html exam','1','20');
$ob->create_exam();
echo 'success';
}
}
class exam_class{
private $id;
private $title;
private $catagory;
private $timeLength;
function set_exam($id,$title,$catagory,$timeLength){
$this->id = $id;
$this->title = $title;
$this->catagory = $catagory;
$this->timeLength = $timeLength;
}
function create_exam(){
$this->load->model('examModel');
$this->examModel->create_exams($title,$catagory,$timeLength);
}
}
model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ExamModel extends CI_Model {
public function create_exams($title,$catagory,$timeLength){
$data = array(
'title' => $title ,
'catagory' => $catagory ,
'timeLength' => $timeLength
);
$this->db->insert('exams', $data);
}
}
error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: exam_class::$load
Filename: controllers/exams.php
Line Number: 26
Fatal error: Call to a member function model() on a non-object in C:\xampp\htdocs\exam\application\controllers\exams.php on line 26
You shouldn't put more than one class in a file. Controller should be something like this.
class Exams extends CI_Controller {
private $id;
private $title;
private $catagory;
private $timeLength;
public function __construct()
{
parent::__construct();
$this->load->model('examModel');
}
public function index(){
$this->set_exam(0,'Html exam','1','20');
$this->create_exam();
echo 'success';
}
function set_exam($id,$title,$catagory,$timeLength){
$this->id = $id;
$this->title = $title;
$this->catagory = $catagory;
$this->timeLength = $timeLength;
}
function create_exam(){
$this->examModel->create_exams($title,$catagory,$timeLength);
}
}
Along with #shin i want to include that go to this file
....\testproject\application\config\autoload.php
and edit this to add your models
$autoload['model'] = array('modelName1','modelName2');
and to load the models from any time from any controller . This will automatically load your models.No need to add
$this->load->model('modelName');
Tip : Keep it simple
In your controller :
public function __construct()
{
parent::__construct();
$this->load->model('examModel');
}
public function index()
{
$exam_data = $this->process_exam_data(0,'Html exam','1','20');
$insert_status = $this->examModel->create_exams($exam_data);
if($insert_status===TRUE){
echo "Exam Insert Successful!";
}
else{
echo "Exam Insert Failed";
}
}
public function process_exam_data($id, $title, $category, $timelength)
{
// Do whatever you want with the data, calculations etc.
// Prepare your data array same as to be inserted into db
$final_data = array(
'title' => $processed_title,
'catagory' => $processed_category,
'timeLength' => $processed_time
);
return $final_data;
}
And in your model :
public function create_exams($data)
{
$result = $this->db->insert('exams', $data); // Query builder functions return true on success and false on failure
return $result;
}
Your index function is the main function which does the calling, whereas all the processing work is done in the process_exam_data function.
Have a nice day :)
I have question to you.
I try add to my page calendar and some events in this calendar. I know how I can load calendar in page, but I didn`t now how I can load this calendar on every page automatically.
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Calendar extends CI_Controller {
function index()
{
$data = array(
3 => 'Polska - Anglia',
);
$this->load->library('calendar');
$vars['calendar'] = $this->calendar->generate('', '', $data);
$this->load->view('main/calendar', $vars);
}
}
and In view I call:
<?php echo $calendar;?>
I use CodeIgniter 2.1.3
Instead of creating controller for calendar, create a library class and then add it to autoload configuration file
class MyCalendar{
public function get()
{
$CI =& get_instance();
$data = array(
3 => 'Polska - Anglia',
);
$CI->load->library('calendar');
return $CI->calendar->generate('', '', $data);
}
}
Add this library to autoload file and then you can call it anywhere you want by using this statement.
$data['calendar'] = $this->MyCalendar->get();
You can autoload your library by changing the application/config/autoload.php file.
Find :
$autoload['libraries'] = array();
Replace by :
$autoload['libraries'] = array('calendar');
To load the same calendar on all your pages, I suggest to build a parent controller in the application/core folder.
abstract class BaseController extends CI_Controller
{
protected $data;
public function __construct()
{
parent::__construct();
$this->data = array();
$calendarData = array(
3 => 'Polska - Anglia'
);
$this->data['calendar'] = $this->calendar->generate('', '', $calendarData);
}
}
You can then extend that BaseController class on all your controllers.
class Calendar extends BaseController {
function index()
{
$this->load->view('main/calendar', $this->data);
}
}
Be sure to always use $this->data to build on the protected member of your class.
Lastly, your BaseController will not be autoloaded, you probably don't want to include it everywhere. I suggest you to add the following method at the end of your autoload or config file.
/**
* Extra autoload function. Loads core classes automatically.
* #param type $class The class to load.
*/
function __autoload($class)
{
if (strpos($class, 'CI_') !== 0)
{
if (file_exists($file = APPPATH . 'core/' . $class . EXT))
{
include $file;
}
}
}
This will allow you to autoload any classes you have in your application/core folder. This solution might seems complex, but once it's setup. You can add functionality to your BaseController that is applicable for all pages, for example, header and footer stuff.
Hope this helps!