cross module view file loading codeigniter - codeigniter

I have two modules in codeigniter hmvc.
1- Acess
2- Display
Here is My Access Module Controller
class Access extends MX_Controller
{
public function __contstruct()
{
parent::__construct();
$this->load->module('display');
}
public function index()
{
echo modules::run('display/login');
}
public function logout()
{
//$this->load->view('login');
echo modules::run('display/test');
}
}
and here is my display module controlelr
class Display extends MX_Controller
{
public function login()
{
$this->load->view('header');
$this->load->view('login'); // This file resides in Access module view folder
$this->load->view('footer');
}
}
So, When acess controller comes in contact, technically it should access display module login function in controller which in return should display the login form along with the header and footer.
Here the problem is that the login.php is placed in access module view file which is being accessed from display module controller. So, I guess the question is pretty much clear for every one.

When loading the view you just need to add the module name before the view name and it will work. So $this->load->view('login'); will become $this->load->view('access/login');
That should work.

Related

Session keep track of whose logged in and only show to members

I'm not really sure what to do when it comes to dealing with session.
After I log in and set a session data.
How do I keep private pages to only show to those who are logged in.
Do I have to run that kind of validation to all my views?
<?php
if($this->session->userdata('is_loggedin')!=1)
{
redirect('KGindex/index','refresh');
}
?>
I'm not even sure if that is correct, right now it messed up my code. functions don't work anymore.
Where do I run session validations?
You want to be doing this check in your controller rather than in the view. For example
class Account extends CI_Controller {
public function index()
{
if($this->session->userdata('is_loggedin')!=1)
{
redirect('KGindex/index','refresh');
}
}
}
If you know that every function in your controller requires the user to be logged in then you could include the check in the __construct() function as this is called whenever the class is accessed. Therefore you would only need the put the code in one place.
class Account extends CI_Controller {
public function __construct()
{
if($this->session->userdata('is_loggedin')!=1)
{
redirect('KGindex/index','refresh');
}
}
public function index()
{
//__construct() has already been called
}
}

White Screen When posting in CodeIgniter

Codeigniter gives a white screen every time a form is posted:
Here is the controller logic [controllers/account.php]:
class Account extends CI_Controller
{
public function create()
{
if($this->input->post(NULL, TRUE)){
$params = $this->input->post();
//add validation layer
$accountOptions = array($params are used here)
$this->load->model('account/account', 'account');
$this->account->initialize($accountOptions);
$this->account->save();
}
$header['title'] = "Create Free Account";
$this->load->view('front_end/header', $header);
$this->load->view('main_content');
$content['account_form'] = $this->load->view('forms/account_form', NULL, TRUE);
$this->load->view('account/create', $content);
$footer['extraJs'] = "account";
$this->load->view('front_end/footer', $footer);
}
}
Here is the Account Model logic [models/account/account.php]:
class Account extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function initialize($options)
{
//initialize
}
}
The view first loads fine then after filling the form and clicking submit, just white page.
I tried to add __construct to the controller and load account/account from there, the form does not even load. Any ideas?
I just found the problem:
- The Model account has duplicated definition and the error_reporting was off!
You shouldn't have two classes with the same name Account (the controller and model). Check your server and/or Codeigniter log it should show up there.
I advise you to call your controller class Account and your model class M_Account. You can then rename the model to account whenever you load it, just like you did:
$this->load->model('account/m_account', 'account');
public function __construct()
{
parent::__construct();
$this->load->model("account/account");
}
you load model,library and helper files in construct dont load to inside a function

How to pass parameters to module constructor when calling a HMVC module in CodeIgniter?

I just want to know on how to pass parameters in module
constructor?
Here is the code that is wrote but its not functioning well.
//Here is the main controller
class Main extends MX_Controller
{
public function _construct()
{
parent::_construct();
}
public function index()
{
// sample parameter
$aparam = array(
'param1' => 'param value1',
'param2' => 'param value2'
);
$this->load->module('dashboard',$aparam);
}
}
// Here is "dashboard" module controller
class Dashboard extends MX_Controller
{
public function __construct($aparam)
{
//output param value
// want to get this value
echo $aparam['param1'];
echo $aparam['param2'];
}
}
Please help. thanks.
Ok, so just to clarify, I don't know what "HMVC" stands for but I did notice that if you are trying to use the codeigniter framework then when you create a controller class you must extend the "CI_Controller" class not the "MX_Controller".
Here is a reference page in the codeigniter manual:
http://codeigniter.com/user_guide/general/controllers.html
If you are trying to create a stand alone class that interacts with your code somehow, Codeigniter allows for this via "libraries". A library is just a class.
Here is the reference page in the codeigniter manual:
http://codeigniter.com/user_guide/general/creating_libraries.html

base controller and apply it to all existing controller

I need to create codeigniter base controller to check allowed ip address in database by mobel function if the ip is exists then user should go to home page but if the ip address is not exists and show 404 page in codeigniter, i can't find core folder in application folder
First, you need to extend a core class, call it MY_Controller.php
Save that file in: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('ip_table_model');
$this->load->library('input');
// assuming there's a function called "check_ip($ip_address)" in ip_table_model
if (!$this->ip_table_model->check_ip($this->input->ip_address()) {
redirect('error_404');
}
}
}
Now, we're assuming you have a model called ip_table_model which connects to database with list of IP addresses, and there's a function called check_ip which will validate whether user has access or not. This is relatively simple, and I won't show any examples on this.
The redirect('error_404'); page does not yet exist, you need to create a controller which shows your 404 page.
Now, for any other controllers in your project, instead of extends CI_Controller, make them extend MY_Controller instead.
Here's an example:
class Welcome extends MY_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('welcome_message');
}
}
Explanation: We're extending CI_Controller to create our own core controller, called MY_Controller. Inside, we're checking if user has access or not through the constructor, which will be called in every other controller in the project.
References:
http://codeigniter.com/user_guide/general/core_classes.html
http://codeigniter.com/user_guide/libraries/input.html
Answer is here (section Extending Core Class).
1.7.2 has a different structure to 2.0.*, therefore there is no core folder in application
In Core Create a new Class .
Name MY_Controller.php
class MY_Controller extends CI_Controller {
// Write your functions here which you wanna use throughout the website
public function abc (){
echo "Helllo";
}
}
class Welcome extends MY_Controller {
function __construct()
{
parent::__construct();
}
function your_custom_fuctions()
{
$this->abc(); //echo Hello...
//Anything you want to do
}
}
function admin_view($view_name = "", $header_info = NULL, $sidebar_info=NULL,$page_info = NULL, $footer_info = NULL, $data_info = ""){
$this->load->view('Admin/includes/header', $header_info);
$this->load->view('Admin/includes/Left_sidebar', $sidebar_info);
$this->load->view($view_name, $page_info);
$this->load->view('common/footer', $footer_info);
}

Load Model in HMVC

I am trying to load a model within the same module from a controller.
$this->load->model('pendingAccountModel');
but the model could not be loaded.
the module dir is accounts.
the model file path is: app/modules/accounts/models/pendingAccountModel.php
the model decleration is:
class PendingAccountModel extends Model {
function __construct(){
parent::__construct();
}
}
this is the controller who loads the model:
class PendingAccount extends MX_Controller {
function __construct(){
parent::__construct();
}
function register($data_arr)
{
$this->load->model('pendingAccountModel');
}
}
CI 1.72 with latest hmvc
Thanks
had a quick read through the HMVC docs ~
$this->load->model('pendingAccountModel');
the docs suggest that you should include the module name in the include path
so try (perhaps) $this->load->model('accounts/pendingAccountModel');
also note your "PendingAccount" controller needs to be in:
app/modules/accounts/controllers/PendingAccount.php

Resources