Codeigniter Page Not Found - Migrating To Live Server - codeigniter

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

Related

CodeIgniter Call to a member function on a non-object in [library]

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?

Not able to load model in Codeigniter

I have a model called register_model.php, which I have loaded in a function in my controller (register_controller.php). Model file is placed into model folder itself. Still, I get this error.
An Error Was Encountered
Unable to locate the model you have specified: register_model
register_controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
// function index()
// {
//
// }
public function register() {
if ($this->session->userdata('logged_in'))
{
//user is already logged in
redirect('index.php');
}
else {
//init
//$data['country_list']=$this->config->item('um_country_list');
$data['username'] = '';
$data['firstname'] = '';
$data['lastname'] = '';
$data['email'] = '';
// $data['password'] = '';
//$data['userlevel'] = '';
//load rules
$rules = $this->config->item('um_register_rules');
//default msg
$data['msg'] = $this->lang->line('um_form_msg');
$this->load->model('register_model');
if (isset($_POST['submit'])) {
//the user has submitted the form
//get the user input
$data['username'] = $this->input->post('username');
$data['firstname'] = $this->input->post('firstname');
$data['lastname'] = $this->input->post('lastname');
$data['email'] = $this->input->post('email');
$data['password'] = $this->input->post('password');
//$data['userlevel'] = $this->input->post('userlevel');
$this->form_validation->set_rules($rules); //check with the rules
if ($this->form_validation->run() === FALSE) {
//validation failed
$data['msg'] = $this->lang->line('um_form_error');
$this->load->view('user_register_form', $data);
} else {
//validation passed
$dbdata = array(
'username' => $this->input->post('username'),
'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
//'userlevel' => $this->input->post('userlevel')
);
$this->register_model->register_user($dbdata);
$data['msg']=$this->lang->line('um_form_activate');
//render the view
$this->load->view('um_msg', $data);
}
} else {
//render the view
$this->load->view('user_register_form', $data);
}
}
}
}
?>
register_model.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Register_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function register_user($dbdata) {
$this->db->insert('users', $dbdata);
}
}
?>
Probably you should include your model inside your controller, try to include after parent::__construct
This should look like so:
class Registration extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('register_model');
}
or if you using this model in most controllers you can include it using autoload, go to /config/autoload.php search for $autoload['model'] and add your model inside the suitable array
First of all load your model in your controller ..
function __construct() {
parent::__construct();
$this->load->model('register_model');
}
This will work for you...

How to pass variables in codeigniter 2

I have following
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Hello extends CI_Controller {
var $name = 'test';
function index() {
$this->name = 'Andy';
$data['name'] = $this->name;
$this->load->view('you_view', $data); // THIS WORKS
}
function you() {
$data['name'] = $this->name;
$this->load->view('you_view', $data); // BUT THIS DOESN'T WORK
}
}
My question is how to I pass the $this->name = 'Andy'; to you() ??
Since it is being set in a different method of the controller, which equates to another request in your code, you will need to store it in a session variable to have it persist across page requests.
function index() {
$this->name = 'Andy';
$data['name'] = $this->name;
$this->session->set_userdata('name', $this->name);
$this->load->view('you_view', $data); // THIS WORKS
}
function you() {
$data['name'] = $this->session->userdata('name');
$this->load->view('you_view', $data); // BUT THIS DOESN'T WORK
}
If its a value that is part of the class you can put it in the constructor
class Hello extends CI_Controller {
public function __construct() {
parent::__construct();
// will be available to any method in the class
$this->name = 'andy';
}

how can i make my own $this->load->view in codeigniter

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");

Passing query result to 2nd function in Codeigniter

I'm trying to run two queries in two functions in a model on Codeigniter. The 2nd query runs based on the result of the first by passing the variable between the functions.
The first query gets the row from the 'photos' database based on the URL the user goes to. The second query is run against the 'users' database and the row is selected based on the 'userid' field from the row of the first query.
I need to be able to get all data from the rows of both queries. I get a 404 error at the moment, any ideas as how to get it working?
Code is as follows:
Model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Viewphoto_model extends CI_Model {
public function get_photo($id)
{
$db_photos = $this->load->database('photos', TRUE);
$db_photos->select('*');
$db_photos->select("DATE_FORMAT(uploaddate, '%d/%m/%y') as uploaddate_formatted", FALSE);
$db_photos->from('photos');
$db_photos->where('approved', '1');
$db_photos->where('id', $id);
$result = $db_photos->get()->row();
$userid = $this->get_user($result->userid);
}
public function get_user($userid)
{
$db_users = $this->load->database('users', TRUE);
$db_users->select('firstname, lastname, email');
$db_users->from('useraccounts');
$db_users->where('id', $userid);
$query = $db_users->get();
return $query->row();
}
}
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Viewphoto extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('viewphoto_model');
}
public function view($id)
{
$data['photo'] = $this->viewphoto_model->get_photo($id);
if (empty($data['photo']))
{
show_404();
}
$data['user'] = $this->viewphoto_model->get_user($userid);
if (empty($data['user']))
{
show_404();
}
$data['title'] = $data['photo']->title.' by '.$data['photo']->username;
$data['meta_description'] = $data['photo']->description;
$data['directory'] = 'sub';
$this->load->view('templates/header', $data);
$this->load->view('viewphoto/viewphoto', $data);
$this->load->view('templates/footer', $data);
}
}
You seem to have a slight logic issue. Your get_photo() function should return the photo object, which your controller can then use in the call to get_user()...
public function get_photo($id)
{
$db_photos = $this->load->database('photos', TRUE);
$db_photos->select('*');
$db_photos->select("DATE_FORMAT(uploaddate, '%d/%m/%y') as uploaddate_formatted", FALSE);
$db_photos->from('photos');
$db_photos->where('approved', '1');
$db_photos->where('id', $id);
return $db_photos->get()->row();
}
Then in your controller....
public function view($id)
{
$data['photo'] = $this->viewphoto_model->get_photo($id);
if (empty($data['photo']))
{
show_404();
}
// Pass the photo's userid here
$data['user'] = $this->viewphoto_model->get_user($data['photo']->userid);
if (empty($data['user']))
{
show_404();
}
// ...
}

Resources