please help me with the below code. I'm trying to assess base_url helper from Mylib(external library)
Class Mylib{
public function url() {
$ci = &get_instance();
$ci->load->helper('url');
return base_url();
}
}
No its works fine. Check this.
class Mylib {
protected $CI;
public function __construct()
{
$this->CI =& get_instance();
}
public function url(){
this->CI->load->helper('url');
redirect();
}
}
Related
I don't know if the title above is an correct title about my question.
Is there a way to declare a variable which we can access it from anywhere in view without need to redefine it again in each function in controller?
for example in controller file Students.php contains many function that handle the views, take a look below :
public function __construct() {
parent::__construct();
$data['count_student'] = $this->m_data->allStudent(); // count all students
}
public function index() {
$data['content'] = 'view-students';
$this->load->view('frontend/header' , $data);
}
public function showDetails() {
$data['content'] = 'view-detail-students';
$this->load->view('frontend/header' , $data);
}
I expected we can access $count_student in both view-students.php and view-detail-student.php without to define $data['count_student'] = $this->m_data->allStudent(); on each function that handle the view.
Is there possible ways to do that?
In the application/core/ create MY_Controller.php
<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('m_data');
$this->data['count_student'] = $this->m_data->allStudent();
}
}
Controller use $this->data
class Students extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->data['content'] = 'view-students';
$this->load->view('frontend/header', $this->data);
}
public function showDetails() {
$this->data['content'] = 'view-detail-students';
$this->load->view('frontend/header', $this->data);
}
}
Now that you have extended the controller you shoudld be able to just go like
<?php echo $count_student;?>
On the view
I noticed that you can access variables in views without passing them if you declare them in the controller with $this->. Probably because they default to public visibility.
public function __construct() {
parent::__construct();
// $data['count_student'] = $this->m_data->allStudent(); // count all students
//
$this->count_all_the_students = $this->m_data->allStudents();
}
public function index() {
$data['content'] = 'view-students';
$this->load->view('frontend/header' , $data);
}
And then in the view you can use $this->count_all_the_students without putting it in the $data array.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<?php
echo 'I have misled ' . $this->count_all_the_students . ' students with my MVC breaking suggestions.';
If you are using the same variable in every controller function then you have to declare it as follows
class Example extends CI_Controller{
protected $data = array();
public function __construct() {
parent::__construct();
$this->data['count_student'] = $this->m_data->allStudent();
}
public function index() {
$this->data['content'] = 'view-students'; //variables are same
$this->load->view('frontend/header' , $this->data);
}
public function showDetails() {
$this->data['content'] = 'view-detail-students'; //variables are same
$this->load->view('frontend/header' , $this->data);
}
}
This applies only if you have common variables for all views in this controller.
If you are using different variables then use the following code.
class Example extends CI_Controller{
protected $count_student= "";
public function __construct() {
parent::__construct();
$this->count_student = $this->m_data->allStudent();
}
public function index() {
$data['content'] = 'view-students'; //Variable is different
$data['count_student'] = $this->count_students;
$this->load->view('frontend/header' , $data);
}
public function showDetails() {
$data['details'] = 'view-detail-students'; // Variable is different
$data['count_student'] = $this->count_students;
$this->load->view('frontend/header' , $data);
}
}
i have a problem with my login in application . in my login authentication a create a library name is "auth" and i also was call "auth" in autoload but when i call a function in "auth" the error say
Message: Undefined property: Auth::$session
here is my library "auth"
class Auth {
public function cek_auth()
{
$this->ci =& get_instance();
$this->sesi = $this->ci->session->userdata('isLogin');
$this->hak = $this->ci->session->userdata('stat');
if($this->sesi != TRUE){
$this->session->set_flashdata("pesan", "Sesi Login anda telah habis");
redirect('user/login');
exit();
}
}
public function hak_akses($kecuali="")
{
if($this->hak==$kecuali){
echo "<script>alert('Anda tidak berhak mengakses halaman ini!');</script>";
redirect('dashboard');
}elseif ($this->hak=="") {
echo "<script>alert('Silahkan Login!');</script>";
redirect('c_login','refresh');
}else{
}
}
}
my controller :
class Dashboard extends CI_Controller{
function __construct()
{
parent::__construct();
$this->auth->cek_auth(); //ngambil auth dari library
}
function index()
{
$hak_akses = $this->session->userdata('lvl');
if($hak_akses==1) {
$data['content'] = 'dashboard';
return $this->load->view('theme/index', $data);
}
}
}
and my autoload :
$autoload['libraries'] = array('database','form_validation','session','auth','parser');
please give me a best solution , thanks
If you are using session in your code then make sure to autoload in config.php file by setting an encryption key. If you don't autoload it then you can load it using $this->load->library('session'). But you must set encryption key in order to autoload or load session in controller functions.
Reference : Undefined property: CI::$session.
Hope this helps :)
Try loading the session library inside
public function __construct()
{
$this->ci =& get_instance();
$this->ci->load->library('session');
$this->sesi = $this->ci->session->userdata('isLogin');
}
I have created a web application with Codeigniter, and I have a problem at the url, controller and structure level.
I have the following web structure.
http://projectroot/admin
Then I have several sections like:
http://projectroot/admin/users
http://projectroot/admin/profile
http://projectroot/admin/section_tracking
http://projectroot/admin/section_products
etc...
I'm working with sessions and other libraries
Currently I have everything in a single controller called Admin, but I would like to create independent controllers that would be calling each part of the url.
In Admin I have:
class Admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('My_PHPMailer');
$this->load->library('session');
}
public function index(){
if($this->session->userdata('login')){
if($this->session->userdata('urlnow')){
$url = $this->session->userdata('urlnow');
redirect($url);
}else{
redirect('admin/index');
}
}else{
$data = array();
$data['usererror'] = $this->session->flashdata('usererror');
$data['passerror'] = $this->session->flashdata('passerror');
$data['message'] = $this->session->flashdata('message');
$this->load->view('admin/index', $data);
}
}
...
public function users() {
code....
}
public function profile() {
code....
}
public function section_tracking() {
code....
}
public function section_products() {
code....
}
My idea is that the controller folder contains something like this:
admin.php
users.php
profile.php
section_products.php
section_visits.php
Creating independent Admin extends classes (user, profile, section_tracking and section_products) as independent controllers outside of admin, with a structure similar to this:
users.php
class Users extends Admin {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('My_PHPMailer');
$this->load->library('session');
}
public function index(){
code here...
}
}
profile.php
class Profile extends Admin {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('My_PHPMailer');
$this->load->library('session');
}
public function index(){
code here...
}
}
How can I do it? I don't want to use HMVC, I just want with MVC native.
Thank you
In your application/controllers/admin folder...Make following files
Admin.php
Users.php
Profile.php
Section_products.php
Section_visits.php
Then make call to each files like this...
http://projectroot/admin/users
http://projectroot/admin/profile
http://projectroot/admin/section_tracking
http://projectroot/admin/section_products
etc...
And here you want to extends admin controller.so make Admin_controller.php by extending CI_Controller in application/core. Your `Admin' controller must be like this....
class Admin_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
}
Then always extends Admin_controller.CI_Controller automatically extended.
I'm a codeigniter beginner. I'm using version 2.0.2 with a local server (php 5.3) and I have a start controller with code like this (just for testing obviously):
<?php
class Start extends CI_Controller
{
var $base;
function _construct()
{
parent::_construct();
$this->base = $this->config->item('base_url');
}
function hello()
{
$data['base'] = $this->base;
print_r ($data);
}
}
When I navigate to the hello function the $data['base'] array element is empty. Why should that be when the construct function has populated it with 'base_url' from the config file?
Seems that the variable $base is not available outside the construct function but I can't understand why or how to fix. Can anyone advise please?
Your constructor should be __construct() (2 underscores).
function __construct()
{
parent::__construct();
$this->base = $this->config->item('base_url');
}
Also, as other people have mentioned, if you load the 'url_helper', you can get the base_url by calling base_url().
$this->load->helper('url');
$this->base = base_url();
Did you know you can do
$this->load->helper('url');
$base = base_url();
Or even in the view:
<?php echo base_url(); ?>
Use it like this:
class Start extends CI_Controller
{
private $base = '';
function _construct()
{
parent::_construct();
$this->base = $this->config->item('base_url');
}
function hello()
{
$data['base'] = $this->base;
print_r ($data);
}
}
Or in the autoload.php configure:
$autoload['helper'] = array('url');
and then you can use base_url(); everywhere in your code.
I know this is probably simple, but I'm not getting. I've created a library, and I want to load the parameters from a config file. So here's an example of what I have:
// libraries/Mylib.php
class Mylib {
var $ci;
var $key;
public function _construct {
$this->ci =& get_instance();
$this->ci->config->load('mylib');
$this->key = $this->ci->config->item('key');
}
public function myKey() {
return "Key=" . $this->key;
}
}
// config/mylib.php
$config['key'] = 'randomcharacters';
I load the library, and try to print out the myKey function, but it just returns "Key=", without the actual key. What am I missing?
It seems like you missed an underscore for your constructor:
instead of
public function _construct () {
you should use
public function __construct () {