passing parameter to view give infinite loop / page refresh in codeigniter 3 - php

I have some problem with CodeIgniter 3.
When I try to access URL with the parameter like these http://localhost/crm/Customer/update/3
The result is infinite loop page refresh.
This does not happen if I access other view or URL.
For example : http://localhost/crm/Customer/list
Below is my controller file :
class Customer extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url', 'customer'));
$this->load->database();
$this->load->library('session');
$this->load->model(array('Auth', 'Model_customer'));
$this->_init();
}
function _init() {
$this->output->set_template('index');
// $this->load->section('sidebar','template/sidebar');
}
public function index(){
...
$this->load->view('customer/index');
}
function update($id) {
// TEST
$data['id'] = $id;
$this->load->view('customer/update2', $data);
}
}
Any help will be valuable. Thanks

Related

Ion Auth + Codeigniter logged_in always redirects to login

I am currently using Codeigniter 3.1.2 with Ion Auth. I can login and view my dashboard page, however if I attempt to view another page that requires the user to be logged in, such as the customer page I am always redirected to the login page.
Controller
public function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->model('CRUD');
if (!$this->ion_auth->logged_in()) {
redirect('User/index');
}
}
public function index(){
$this->load->helper('form');
$this->load->view('login');
}
public function dashboard(){
$this->load->view('dashboard');
}
public function customerPage(){
$data['customer'] = $this->CRUD->getCustomers();
$this->load->view('customer', $data);
}
public function materialPage(){
$data['material'] = $this->CRUD->getMaterials();
$this->load->view('material', $data);
}
Thanks,
CHTRK
public function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->model('CRUD');
if (!$this->ion_auth->logged_in()) {
redirect('User/index');
}
"!$this->ion_auth->logged_in()" use this outside the constructor because it will run every time. you should create a different method to call every time in the methods. don't use it for index

How to pass the data to view in codeigniter

Suppose i have a controller called home like this
class Home extends CI_Controller{
public function __construct()
{
parent::__construct();
//load the settings model
$this->load->Model('settings_model');
//get the all settings and store it into a variable
$siteSettings = $this->settings_model->SiteSettings();
//how to pass the data to view
$data['site'] = $siteSettings;
}
public function index()
{
//some code and pass the value to view
$data["some_item"] = "some value";
//Load the view
$this->load->view("home",$data);
}
public function SomeMethod1()
{
//some code and pass the value to view
$data["some_item"] = "some value";
//Load the view
$this->load->view("home",$data);
}
public function SomeMethod2()
{
//some code and pass the value to view
$data["some_item"] = "some value";
//Load the view
$this->load->view("home",$data);
}
}
But my problem is i want to pass $siteSettings to my each method. I don't want to fetch the data every time from settings_model for my different method of home controller. I just want to get the data from database on __construct() and pass the value to each method when i call the different method.
How can i achieve this? Should I use a public variable and store the fetched data to this variable and call the variable from different method?
Set that in session. and can access all over the project.
public function __construct()
{
parent::__construct();
$this->session->unset_userdata('site'); # unset on each and every time
$this->load->Model('settings_model');
$siteSettings = $this->settings_model->SiteSettings();
$this->session->set_userdata('site', $siteSettings); # Set after end of __construct
}
Or do like this
public function __construct()
{
parent::__construct();
$this->load->Model('settings_model');
$this->data['site'] = $this->settings_model->SiteSettings();
}
In function
$this->load->view("name",$this->data);

How to access a controller method from view in CodeIgniter?

I want to do something like this in CodeIgniter:
Controller:
class Home extends CI_Controller {
public function index() {
$this->load->view('home');
}
public function user($user) {
return $user;
}
}
View (home):
Hi <?php echo user('User') ?> // call a controller method
How can i do this?
One way to get it is , returning it from the routes.php ,
Like $route['user'] = "home/user"; And this will execute the user method from the home controller.

Codeigniter Why I can't load view from MY_Controller

I want every controller to have a method _render_page, which loads the master template and passes the data object.
My home controller class looks like this:
class Home extends MY_Controller {
function __construct() {
parent::__construct();
}
public function index() {
$data['title'] = "Site title";
$data['the_view'] = 'welcome_message';
$this->_render_page($this->layout, $data);
//$this->load->view($this->layout, $data); //This works ok..
}
}
MY_controller class:
class MY_Controller extends CI_Controller {
public $layout;
public $viewdata;
public function __construct() {
parent::__construct();
$this->layout = 'layout/master_template';
$this->viewdata = null;
}
public function _render_page($view, $data=null, $render=false) {
$this->viewdata = $data;
$this->viewdata['the_view'] = $view;
if($this->ion_auth->logged_in()) {
$user_obj = $this->ion_auth->user()->row();
$usr_data['username'] = $user_obj->username;
$user_obj = null;
$this->viewdata['usr_data'] = $usr_data;
}
$this->load->view($this->layout, $this->viewdata); //The code crashes here
}
}
When I browse to home controller I get nothing, just white screen no errors...
Found a solution: I'm calling _render_page in a wrong way.
Instead of this:
$this->_render_page($this->layout, $data);
I should call like this:
$this->_render_page('welcome_message', $data);
Of course, that's what this function is about - to load a master page and pass the view name as member of $data, so the master page will know which view to load.
See, you need to understand the flow going,
when you call your class home , it extends MY_Controller, CI will look for MY_Controller, the constructor of your MY_controller gets executed after which CI starts executing the constructor of your home controller then your default method of home controller,
so in order to make it work you need to call _render_page()-
change your MY_Controller like -
class MY_Controller extends CI_Controller {
public $layout;
public $viewdata;
public function __construct() {
parent::__construct();
$this->layout = 'layout/master_template';
$this->viewdata = null;
$this->_render_page($this->layout, $data=null, $render=false); // call your method
}
public function _render_page($view, $data=null, $render=false) {
$this->viewdata = $data;
$this->viewdata['the_view'] = $view;
if($this->ion_auth->logged_in()) {
$user_obj = $this->ion_auth->user()->row();
$usr_data['username'] = $user_obj->username;
$user_obj = null;
$this->viewdata['usr_data'] = $usr_data;
}
$this->load->view($this->layout, $this->viewdata); //The code crashes here
}
}

code igniter loading a view at the end of the controller

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();
}
}

Categories