when i code href="<?= base_url(loket/index_pos_bakum) ?>" then run it, it run normali. im directed to base_url/loket/index_pos_bakum. but the problem comes when i type code like before with deffrent value, base_url(admin/loket_admin). it doesnt gonna redirect to admin/loket_admin but its redirect to base_url/loket/admin/loket_admin. the first controller name always appear when im gonna try to redirect to any controller.
defined('BASEPATH') or exit('No direct script access allowed');
class Loket extends CI_Controller
{
public function index_pos_bakum()
{
$data['loket'] = $this->db->get_where('nomor_antrian', ['id_loket' => 0])->result_array();
$data['antrian'] = $this->db->query("SELECT MAX(nomor) AS nomor FROM nomor_antrian WHERE id_loket = 0 ")->row_array();
$this->template->load('template', 'operator/v_pos_yankum', $data);
}
}
And this is my second controller
class Admin extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Admin_model', 'a_model');
}
public function loket_admin()
{
$this->template->load('template','admin/index_loket');
}
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");
}
}
I have code in controller Codeigniter:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct()
{
parent:: __construct();
$this->load->model('berita');
}
public function index()
{
$data['newsone'] = $this->berita->get_one_top_data();
if(width.screen > 1000px){ // i need the true code in here
$this->load->view('home_view_1',$data);
}
else{
$this->load->view('home_view_2',$data);
}
}
}
how can i open specific page depend on screen size...?
I have this on my MY_Controller that was located on my core folder.
<?php
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function is_logged_in($data){
$session = $this->session->userdata();
if($session['isloggedin']['username'] == ''){
return isset($session);
}else{
return FALSE;}
}
}
?>
I'm pretty sure i copy pasted the above code from some tutorial and i haven't gotten to editing it based on my needs.
Any case i have questions.
So i have a pages controller that will be responsible for giving access to some views depending on the account_type of the logged in user.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class pages extends MY_Controller {
}?>
this is my session whenever a user logs in.
$new_session = array( 'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'type' => $this->input->post('type'),
'logged_in' => TRUE);
$this->session->set_userdata($new_session);
How do i call the MY_controller function is_logged_in() from the pages controller or is the 'extends MY_Controller' automatically also calls the function is_logged_in() or do i have to basically just put it in a __construct so it automatically calls the function?
Also, how do i go about checking if a user is logged in and seeing their details?
Do i pass session_data from my controller to MY_Controller? if so, how?
Or should i just put a $this->session->userdata(); line inside the is_logged_in() function?
P.S. i have tried using Authentication Libraries but they include far too much to what i need, i just need a basic authentication. Any suggestions? that is still maintained right now.
you can directly call is_logged_in() function from your pages controller. just like this:
Pages.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends MY_Controller {
function __construct() {
parent::__construct(); // this will trigger the __construct() of MY_Controller
}
}
MY_Controller.php
<?php
class MY_Controller extends CI_Controller{
public function __construct() {
parent::__construct();
if( $this->is_logged_in() ) {
// do something if user is allowed to access.
}
else {
// do something if user is not allowed to access
}
}
}
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 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 */