On Ci you have the possibility to load a view directly from the constructor of your controller, I'm loading the header and footer of my page (since it's the same for each function)
class Add extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->view('header_view');
$this->load->view('footer_view');
}
function whatever()
{
//do stuff
}
}
But this will load the footer view before loading my function, so is there any way to do it without "manually" loading the view at the end of each function ?
I would add the header/footer in the main view with the data, or use a template library (I use this one).
If in main view for function;
// in view for html page
<?php $this->load->view('header'); ?>
<h1>My Page</h1>
<?php $this->load->view('footer'); ?>
You shouldn't be rendering any views in the constructor. CI Controllers should look more something like this:
class Add extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
function index()
{
$this->load->view('header_view');
$this->load->view('home_page');
$this->load->view('footer_view');
}
function whatever()
{
/*
* Some logic stuff
*/
$data_for_view = array(
'product' => 'thing',
'foo' => 'bar'
);
$this->load->view('header_view');
$this->load->view('show_other_stuff', $data_for_view);
$this->load->view('footer_view');
}
}
I've come up with this approach:
class Add extends CI_Controller{
public function __construct()
{
parent::__construct();
// load some static
$this->data['page_footer'] = $this->common_model->get_footer();
}
private function view_loader () {
//decide what to load based on local environment
if(isset($_SESSION['user'])){
$this->load->view('profile_view', $this->data);
} else {
$this->load->view('unlogged_view', $this->data);
}
}
function index()
{
$this->data['page_content'] = $this->profile_model->do_stuff();
// call once in every function. this is the only thing to repeat.
$this->view_loader();
}
}
Related
In my header view I wrote this code:
<?php
if($this->session->userdata('logged_in')) {
$query = $this->db->get_where('instructors', array('id' => $this->session->userdata('id')));
$insdatacheck = $query->row_array();
if($insdatacheck['name'] == '') {
redirect(base_url().'user/continueregistration');
} else { ?>
<script type="text/javascript">alert('test');</script>
<?php
}
}
?>
But it does not redirect to the following page. However, if I write this in the controller, it works properly. I wrote it in header view because I want to check it in every page where enters the user. How can I improve it and write in a proper way? Thanks in advance
I think instead of your header you should put your check inside your controller constructor.
class Test extends CI_Controller {
function __construct() {
parent::__construct();
// if not logged-in redirect to login page
if ($this->session->userdata('logged_in') == false) {
redirect('login'); // where you want to redirect
}
}
}
Another option is to create a base controller. Place the function in the base controller and then inherit from this.
To achieve this in CodeIgniter, create a file called MY_Controller.php in the libraries folder of your application.
class MY_Controller extends Controller
{
public function __construct()
{
parent::__construct();
}
public function is_logged_in()
{
$user = $this->session->userdata('user_data');
return isset($user);
}
}
Then make your controller inherit from this base controller.
class X extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function do_something()
{
if ($this->is_logged_in())
{
// User is logged in. Do something.
}
}
}
First create session in the controller only,
Then we access session in any page,
$this->load->library('session');
$user=$this->session->userdata('logged_in');
if (!isset($user)) {
redirect(base_url().'user/continueregistration');
}
else {
<script type="text/javascript">alert('test');</script>
}
I have two layout templates in my project and always loading first template. How to load second template also.
In main Usercontroller how to differentiate them. They are,
Learncontroller.php
class Learncontroller extends Usercontroller{
public function __construct(){
parent::__construct();
$this->load->model("Usermodel","",true);
}
public function index(){
$id=$this->session->userdata('cp_userid');
$menuActive= "learning";
$data['menuActive'] = $menuActive;
$userdetails=$this->Usermodel->getuserdetails($id);
$data['userdetails']=$userdetails;
$result=$this->Usermodel->currentlearningcourses($id);
$data['details']=$result;
$data['content']=$this->load->view("user/currentlearningcourses",$data,true);
$data['title']='My Courses';
//$this->load->view("user/layout",$data);
$headerContent = $this->load->view("user/layout",$data,true);
$this->render($headerContent);
}
}
Unitcontroller.php
class Unitcontroller extends Usercontroller{
public function __construct(){
parent::__construct();
$this->load->model("Usermodel","",true);
}
public function courselearn($id){
$r=$this->Usermodel->getsectiontopic($id);
$data['topicId']=$id;
$data['topicQ']=$r;
$data['video']=$this->input->post("id");
$data['id']=$id;
$data['content']=$this->load->view("user/unit_content",$data, true);
$courselearnheaderContent = $this->load->view("user/courselearn_layout",$data,true);
$this->render($courselearnheaderContent);
}
}
Learncontroller and Unitcontroller has separate layouts but always loading protected $layout = 'user/layout';. How to load this 'user/courselearn_layout'; when control comes from Unitcontroller.
Usercontroller.php
protected $layout = 'user/layout';
protected $courselearn_layout = 'user/courselearn_layout';
protected function render($headerContent) {
$view_data = array( 'headerContent' => $headerContent);
$this->load->view($this->layout);
}
Always loading first page. How to make load the second page also please help me.
What I'm trying to attempt is to display a template on a custom tab.
This is the code of controllers/admin/AdminTController.php:
<?php
class AdminTController extends ModuleAdminController {
public function __construct() {
parent::__construct();
}
public function display() {
//echo $this->l('This is admin my module tab !');
// the echo works.
$this->createTemplate('initial.tpl');
}
}
This is code of /views/templates/admin/initial.tpl:
<p>This is just a test</p>
The result of this code is a blank page.This means the template is not loaded. What am I doing wrong?
Try using the below mentioned code.
<?php
class AdminTController extends ModuleAdminController {
public function __construct() {
parent::__construct();
}
public function display() {
$tpl = $this->custom_smarty->createTemplate('initial.tpl');
return $tpl->fetch();
}
}
?>
I am using hierarchical MVC model in codeigniter. I create a controller called template and inside it a function called sample_template. Then a view called sample_template_v created and call it inside the template controller. I create another controller called Admin and called Template->sample_template($data); inside its 2 funtions.
MY_Controlle.php
<?php
class MY_Controller extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->module('Template');
}
}
Admin.php
<?php
class Admin extends MY_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$data['content_view'] = 'admin/admin_v';
$this->Template->sample_template($data);
}
function about()
{
$data['content_view'] = 'admin/about_v';
$this->Template->sample_template($data);
}
}
Template.php
<?php
class Template extends MY_Controller
{
function __construct()
{
parent::__construct();
}
function sample_template($data = NULL)
{
$this->load->view('Template/sample_template_v', $data);
}
}
sample_template_v.php file---->
<h5>This is the main Template.</h5>
<?php $this->load->view($content_view); ?>
Error:
If you want to call a method from the object you need to initiaze the object and then call a method. Make sure $this->template is set in your case it isn't.
$this->template = new Template();
$this->template->sample_template($data);
I have this private session in one of my controllers that checks if a user is logged in:
function _is_logged_in() {
$user = $this->session->userdata('user_data');
if (!isset($user)) {
return false;
}
else {
return true;
}
}
Problem is that I have more than one Controller. How can I use this function in those other controllers? Redefining the function in every Controller isn't very 'DRY'.
Any ideas?
Another option is to create a base controller. Place the function in the base controller and then inherit from this.
To achieve this in CodeIgniter, create a file called MY_Controller.php in the libraries folder of your application.
class MY_Controller extends Controller
{
public function __construct()
{
parent::__construct();
}
public function is_logged_in()
{
$user = $this->session->userdata('user_data');
return isset($user);
}
}
Then make your controller inherit from this base controller.
class X extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function do_something()
{
if ($this->is_logged_in())
{
// User is logged in. Do something.
}
}
}
Put it in a helper and autoload it.
helpers/login_helper.php:
function is_logged_in() {
// Get current CodeIgniter instance
$CI =& get_instance();
// We need to use $CI->session instead of $this->session
$user = $CI->session->userdata('user_data');
if (!isset($user)) { return false; } else { return true; }
}
config/autoload.php:
$autoload['helper'] = array('login');
Then in your controller you can call:
is_logged_in();
You can achieve this using helper and CodeIgniter constructor.
You can create custom helper my_helper.php in that write your function
function is_logged_in() {
$user = $this->session->userdata('user_data');
if (!isset($user)) {
return false;
}
else {
return true;
}
}
In controller if its login.php
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
if(!is_logged_in()) // if you add in constructor no need write each function in above controller.
{
//redirect you login view
}
}
I think using hooks is pretty easy. Just create a hook to check $this->session->user. It will be called in every request.
Get all user's data from session.
In the Controller,
$userData = $this->session->all_userdata();
In the View,
print_r($userData);
I coded like this according to above answers.. And this is running for me
Create file my_helper.php
<?php
function _is_logged_in() {
if(isset($_SESSION['username'])){
return true;
} else {
return false;
}
}
?>
Edit in autoload.php file
$autoload['helper'] = array('my');
In your Controller file
class Welcome extends CI_Controller {
public function __construct(){
parent::__construct();
if(!_is_logged_in())
{
redirect("Login");
}
}
}
Just add this on your folder core file ci_controller at function __construct() to check all controller ():
function __construct()
{
parent::__construct();
if(! $user = $this->session->userdata('user_data');)
{
return false;
}
}