open cart how to get current customer info? - php

I have created following controller:
class ControllerCommonTestSMS extends Controller
{
function index()
{
// action
}
}
But if I add the following line, it throws an error that the function is undefined:
$this->customer->isLogged();

class ControllerCommonTestSMS extends Controller
{
function index()
{
if (!$this->customer->isLogged()) {
//code to be execu
}
}
}
this is working fine.

Related

Wordpress add_action from another class

This is my code:
class Show
{
public function error()
{
add_action('admin_notices', [$this, 'pip1']);
}
public function pip1() {
echo '
<div class="notice notice-success"><p>test</p></div>
';
}
}
(new Show())->error();
Top code works fine and print errors.
But if I use this code in another class, it doesn't work.
Example:
class Manager extends Controller
{
public function add()
{
(new Show())->error();
}
}
(new Manager())->add();
You should include class file first before call it.
class Manager extends Controller
{
public function add()
{
require_once('path/to/class_Show/Show.php');
(new Show())->error();
}
}
(new Manager())->add();

How to redirect from view to controller in codeigniter?

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>
}

Unable to locate the specified class: Session.php, When call a function from another controller in Codeigniter

contrller:News.php
This is my controller News
<?php class News extends CI_Controller {
public function __construct()
{
}
public function getShowIN_News()
{
return $result;
} } ?>
contrller:Category.php
This is my controller Category
<?php class Category extends CI_Controller {
public function __construct()
{
}
public function category()
{
require('news.php');
$test = new News();
$data["headlines"] = $test->getShowIN_News();
} }?>
By using an empty constructor, you're making it so that CI_Controller::__construct() isn't called, and that's where everything in the framework is initialized.
I know you've put it there to hack it so you can call one controller from another, but it is very intentionally made that way, exactly so you don't do this.

Codeigniter calling model method from another model causes error

I have a controller:
class Blah extends Controller
{
function Blah()
{
$this->load->model('baga_model');
}
}
then comes baga_model:
class Baga_model extends Model
{
function do_it()
{
echo "BOOM!";
}
}
..and
class Blah_model extends Model
{
function some_action()
{
$this->baga_model->do_it();
}
}
So .. when in blah_model I call $this->baga_model->do_it() I get an error :
Call to a member function do_it() on a non-object
I just can't understand why.... I know it must work, I did something similar before..
Thanks
Got it! I had to load baga_model in blah_model constructor. This way it works.
Thanks everyone.
public function test()
{
$this->load->model('baga_model');
$this->baga_model->do_it();
}
Model
class baga_model extends CI_Model
{
public function do_it()
{
echo $this->bar("BOOM!");
}
Your not loading your required model inside your model:
class Blah_model extends CI_Model
{
$this->baga_model = $this->load->model('baga_model', true);
public function some_action()
{
$this->baga_model->do_it();
}
}

codeigniter method error

I'm using the following from the controller to call a method from the model but receiving and error:
//from the controller:(main.php)
<?php
class Main extends CI_Controller {
public function __construct() {
parent::__construct();
}
function index() {
.....
$this->load->view('view_form');
}//END Fn index()
function get_th() {
//$the=$this->input->post('th', TRUE);
$d['shit']=$this->model_data->tst();
$this->load->view('view_form',$d);
}//END Fn get_th()
}//END Cls Main
?>
//from the model:(model_data.php)
<?php
class Model_data extends CI_Model {
function slider() {
...
}//END Fn slider()
function check_input($data) {
...
}//END Fn check_input()
function tst() {
$tsts= "hellos";
return $this->tsts;
}
}//END Cls model_data
?>
$autoload['model'] = array('model_data');
The error:
Fatal error: Call to undefined method Model_data::tst() in ... application\controllers\main.php...
i think you forgot to load the model in the controller.
$this->load->model('Model_name');
function get_th() {
$this->load->model('model_data');
$d['shit']=$this->model_data->tst();
$this->load->view('view_form',$d);
}
FIXED :/ can t believe I had an additional bracket at the the end of a long file :( wtf
"}"<-- this was the problem.
btw as I said #pramodhkumar use autoload.php.. I had the model autoload so no need for $this->load->...

Categories