Can not load url helper in codeigniter - php

I tried to load base_url() in controller, but codeigniter does not load the helper('url'). I also call helper from autoload and the constructor both in the hook, but it's still not working and showing an error "Trying to get property of non-object".
Any idea how can I redirect?
My code:
if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
class Auth_hook {
protected $CI;
public function __construct() {
$this->CI =& get_instance();
$this->CI->load->helper('url');
}
public function index(){
redirect(base_url('auth/login'));
print_r("hello!!");
if(isset($_SESSION['name']) == 'TRUE'){
redirect(base_url('auth/admin'));
}
else {
redirect(base_url('auth/login'));
}
}
}

How about this:
class Auth_hook {
protected $CI;
public function __construct() {
$this->CI =& get_instance();
}
public function index(){
// can communicate back with CI by using $this->CI
$this->CI->load->helper('url');
redirect(base_url('auth/login'));
print_r("hello!!");
if(isset($_SESSION['name']) == 'TRUE'){
redirect(base_url('auth/admin'));
}
else {
redirect(base_url('auth/login'));
}
}
}

Related

Can't call parent method properties from inheritance class in php

I'am begginer in OOP PHP. I have code like this
class Index
{
public $activepage = true;
public $url;
public $page;
function __construct()
{
if ($this->activepage) {
$this->url = "Yes";
$this->page = "Home";
} else {
$this->url = "No";
$this->page = "Index";
}
}
public function show()
{
return $this->page;
}
public function showTest()
{
return "test";
}
}
class Home extends Index
{
function __construct()
{
echo $this->show();
}
}
$page = new Home;
My questions is :
Why I have blank page when I invoke Home class?
But when I change constructor in Home class like this echo $this->showTest();, it works. and displaying "test" on screen.
and what actually diferrent between my show method and showTest method in Index class?
When you add a __construct() in the Home class it overrides the construct from the parent class Index.
You can invoke the parent construct manually with:
function __construct()
{
parent::__construct();
echo $this->show();
}
You can call Parent class method like;
parent::show()

Use controller from another controller CodeIgniter and HMVC

I want to load a function from another controller. This is my structure:
- modules
--orderpages
---controllers
----WebshopCore.php
----WebshopController.php
My function insertItemInCart in WebshopController.php is called. But when i want to execute a function from another controller it crashes.
class WebshopController extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->module('orderPages/WebshopCore');
}
function insertItemInCart(){
$partId = $this->input->post('partId');
$quantity = $this->input->post('quantity');
$output = $this->WebshopCore->getPickLocations($partId,$quantity);
}
}
My WebshopCore:
class WebshopCore extends MX_Controller {
public function __construct() {
parent::__construct();
}
public function getPickLocations($partId,$amount){
$result = "test";
return $result;
}
}
What goes wrong? I don't get it
The solution:
$output = modules::load('orderPages/WebshopCore/')->getPickLocations($partId,$quantity);
You should write a library in that case.
In application/libraries create Cart.php (or whatever you want)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cart
{
protected $ci;
public function __construct()
{
$this->ci =& get_instance();
}
public function getPickLocations($partId, $qty)
{
//Your stuff
}
}
And then in your controllers :
$this->load->library("cart");
$data = $this->cart->getPickLocations($this->input->post('partId'), $this->input->post('quantity'));

My custom library function is not working

I am gettig a error on my library
Undefined property: Authenticate::$ci
Here is my custom library function
function is_logged_in() {
$sessionid = $this->ci->session->userdata('moderId');
if($sessionid) {
return isset($sessionid);
} else if(!$sessionid) {
redirect(base_url() . 'moderator');
}
}
Here is my controller
class B2bcategory extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('moderator/b2bcategory_model');
$this->authenticate->is_logged_in();
}
}
I did not see if you had loaded the get_instance() in your construct area of library, Try some thing like this in your authenticate library
Filename: Authenticate.php
<?php
class Authenticate {
public function __construct() {
$this->CI =& get_instance();
}
function is_logged_in() {
$sessionid = $this->CI->session->userdata('moderId');
if($sessionid) {
return isset($sessionid);
} else if(!$sessionid) {
redirect(base_url() . 'moderator');
}
}
}
I have found that seems to work better with $this->CI on your library with get_instance.
To load library on controller if you have not autoload it use.
Filename: B2bcategory.php
<?php
class B2bcategory extends CI_Controller {
public function __construct() {
parent::__construct();
// Or Auto load it
$this->load->library('authenticate');
$this->load->model('moderator/b2bcategory_model');
$this->authenticate->is_logged_in();
}
}
$this->load->model('moderator/b2bcategory_model', 'authenticate');
$this->load->model('REAL_MODEL_PATH', 'PROPERTY_IN_CONTROLLER')
USAGE $this->PROPERTY_IN_CONTROLLER->library_method()
but best way is in application/core create ModeratorController.php
ModeratorController extends CI_Controller
{
public function __construct()
{
parent::__construct();
if($this->session->userdata('moder_id') === false)
{
redirect('site/moder_login');
}
}
}
And all moder controllers extend from this controller

Codeigniter 3 with get_instance(); in hook file

I have a class in hook folder like this
class CheckAuth {
protected $CI;
public function __construct()
{
$this->CI = get_instance();
}
public function check()
{
$router =& load_class('Router', 'core');
// $controller = $this->CI->router->class;
$controller = $router->fetch_class();
$method = $router->fetch_method();
if($controller!='auth')
{
echo $this->CI->userdata('admin_id');
}
}
}
I show error when I get a session
Fatal error: Call to a member function userdata() on a non-object $this->CI return null.
Try this it will work.
public function __construct()
{
}
public function check()
{
$this->CI = get_instance();
$router =& load_class('Router', 'core');
// $controller = $this->CI->router->class;
$controller = $router->fetch_class();
$method = $router->fetch_method();
if($controller!='auth')
{
echo $this->CI->userdata('admin_id');
}
}
That's because CI isn't loaded yet in some hooks points (pre_controller, pre_system). You are probably trying to load class in some of these.

Auth Component's user function not working in case multiple routing

I have routed my site as admin moderator. Means When I use Auth component my sessions are created as Auth->Admin->id & Auth->Moderator->id And Auth->User->id this way.
Now I tried function $this->Auth->user its returning null why??
As I know $this->Auth->user returns session.
I know the traditional way of $this->Session->reads('Auth.Admin') and else but I want to use Auth->user function why its not working
Please use this to get user session data.
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
public function index() {
pr($this->Auth->User());
}
auth_helper:<?php
defined('BASEPATH') OR exit('No direct script access allowed');
if ( ! function_exists('auth'))
{
function auth()
{
$CI =& get_instance();
$CI->load->model('Auth_model');
return $CI->Auth_model;
}
function Adminauth($admin_id)
{
if($admin_id == false)
{
redirect('login');
}
}
}
auth_model:
<?php
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Auth_model extends CI_Model {
private $user = null;
private $ci = null;
public function __construct() {
parent::__construct();
$CI =& get_instance();
$this->ci = $CI;
}
public function get($name, $default = null){
if ($this->ci->session->has_userdata('auth.Adminuser_id')) {
$user_id = $this->ci->session->userdata('auth.Adminuser_id');
$this->user = $this->db->query("SELECT * FROM members WHERE u_id=? LIMIT 1", array($user_id))->row();
}
return !empty($this->user) ? $this->user->u_name : $default;
}
public function is_logged(){
return $this->ci->session->has_userdata('auth.Adminuser_id');
}
public function login($user_id){
return $this->ci->session->set_userdata('auth.Adminuser_id', $user_id);
}
public function logout(){
return $this->ci->session->unset_userdata('auth.Adminuser_id');
}
}

Categories