CI not loading library - codeigniter

Here's my controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('user_model');
}
public function index() {
redirect('user/login');
}
public function login() {
$this->load->library('form_validation', '', 'fv');
var_dump($this);
$this->fv->set_rules('nick_name', 'Nick Name', 'trim|required|max_length[12]|min_length[4]');
$this->fv->set_rules('password', 'Password', 'trim|required|md5');
}
}
?>
I was basically writing a login controller. In the login function, after this I load the view, etc. But up till here, I am getting the following error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: User::$fv
Filename: controllers/user.php
Line Number: 23
I have commented out everything else in the controller effectively to what I have pasted above. I don't get where the problem is. Thanks.
[EDIT]
On var dumping the $this object, I found an even more unusual thing. The object "fv" was not defined inside the $this object but rather under the $this->user_model object. So isset($this->fv) was returning false and isset($this->user_model->fv) was returning true. And yes I tried $this->load->library('form_validation') instead of naming it "fv", results remained the same.

Your controller name should named as user.php & it should located inside the controllers folder.
Try CI recommended coding styles, such as,
$this->load->library('form_validation');
$this->form_validation->set_rules(); etc..

Related

CI_Template' not found

I want to load model using
$this->load->model('apotek_data');
function __construct()
{
parent::__construct();
$this->load->model('apotek_data');
$this->template->write_view('sidenavs', 'template/default_sidenavs', true);
$this->template->write_view('navs', 'template/default_topnavs.php', true);
$this->load->database();
}
But when I put it ($this->load->model) in my code, the view doesn't show, and display an error message.
Fatal error: Class 'CI_Template' not found in
D:\wamp64\www\apotek\system\core\Common.php on line 196
What should I do?
Load the template library:
$this->load->library('template');
OR:
In config/autoload.php add template in the libraries array.
I don't know the name of your library so I am assuming it is template if it is not - change it with the name of the library.
For any other person experiencing a 'CI_Template not found' error:
In my case, I was loading a library (in the constructor) with the same name as the controller that was being accessed.
class Authld extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('Authld');
}
}
I had to change the name of the controller for the issue to be resolved.
class Authentication extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('Authld');
}
}
Your Template.php maybe looks like this:
class Template extends MX_Controller{
If so, change the MX to CI (CI_Controller).

Model Fails To Load

I am having issues with my Code Ignitor, in that I load my model the code fails, without leaving any trace.
I am attempting to load my model like so
<!--CONTROLLER-->
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Concierge extends Public_Controller {
public function submit()
{
$this->load->model('Concierge_model');
}
My model is set up as below:
<!--MODEL-->
<?php
class Concierge_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
function saveRequest($action, $owner)
{
$query = $this->db->query('select * from table');
return $query;
}
Does anyone see what might be the issue that is causing my problem? I am using CodeIgnitor 2.1.2.
Assuming Public_Controller is something custom, make sure that class extends CI_Controller and also make sure the parent's contruct function is called there.
class Public_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
}
I don't know what the problem was, I deleted the model, view, and controller and recreated them. This fixed the issue, but sadly didn't shed any light onto what caused it.

Codeigniter HMVC Modular Error

I'm new to CI + MX and I tried the Modules::run(); style but I can't seem to let it work.
Here's my directory structure:
/application
-/modules
--/main
---/controllers
----main.php
---/models
---/views
--/connections
---/controllers
----connections.php
---/models
----/group_model.php
---/views
----connection_view.php
main.php controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends MX_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
}
function index(){
echo modules::run('connections/load_connections');
}
}
?>
connections.php controller:
<?php
class Connections extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->model('connections/group_model');
}
function load_connections(){
$user_id = 2;
$data['tabs'] = $this->group_model->get_groups($user_id);
$this->load->view('connection_view', $data);
}
}
?>
group_model.php model:
class Group_model extends CI_Model{
function __construct(){
parent::__construct();
}
/**
* Get all groups in db
**/
function get_groups($user_id){
$this->db->select('g.group_name');
$this->db->from('groups AS g');
$this->db->join('members AS m', 'g.group_id = m.group_id');
$this->db->where('m.user_id', $user_id);
return $this->db->get()->result_array();
}
}
My connection_view.php view contains a div and some php codes to display the $data['tabs'] passed as array in load_connections function.
The problem is, I'm getting an error that says:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Connections::$group_model
Filename: controllers/connections.php
Line Number: 14
and
Fatal error: Call to a member function get_groups() on a non-object in C:\xampp\htdocs\hmvc\application\modules\connections\controllers\connections.php on line 14
I have clearly followed all the instructions provided on MX wiki at https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home
and set up everything as needed.
My database.php under /application/config is already configured.
routes.php is also configured pointing the default controller to main.php
I wonder what I have missed or have done wrong that I can't get it to work.
Codeigniter version: 2.1.3
MX version: 5.4
almost missed it, extend connections with MX_Controller since you are calling modules::run(). Whenever you want a controller to be called with modules::run(), you extend it with MX_Controller instead of CI_Controller
Your second error is caused because of your first error.
Your first error is most likely caused because it cant open the group_model.
try this
$this->load->model('group_model');

Error message on callback function not show

I have the below code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('users/user_model');
$this->load->library('form_validation');
}
function _load_login_page()
{
$this->load->model('sysconfig/sysconfig_model');
$a = $this->sysconfig_model->get_sysconfig();
$row = $a[0];
$this->lang->load('klas',$row->sysconfig_language);
$data['sys_name'] = $row->sysconfig_system_name;
$data['template_name'] = $row->systemplate_name;
$data['css_script'] = base_url().'assets/'.$row->systemplate_name;
if($row->sysconfig_maintenance === 'Y')
{
$this->load->view('sysconfig/maintenance',$data);
}
else {
$this->load->view('login',$data);
}
}
function index()
{
$this->form_validation->set_rules('username', 'Username', 'trim|required|max_length[12]|xss_clean|callback_check_auth');
$this->form_validation->set_rules('password','Password','trim|required');
if($this->form_validation->run($this) == FALSE)
{
$this->_load_login_page();
} else {
redirect('welcome','refresh');
}
}
function check_auth()
{
if($this->user_model->authentication())
{
return TRUE;
}
$this->form_validation->set_message('check_auth',$this->lang->line('invalid_username'));
return FALSE;
}
?>
user_model.php
<?php
class User_Model extends CI_Model
{
function authentication()
{
$this->db->where('useracc_id', $this->input->post('username'));
$this->db->where('useracc_password', md5($this->input->post('password')));
$q = $this->db->get('base_useracc');
if($q->num_rows() == 1)
{
$session_data = array('isUserLogged'=>TRUE);
$this->session->set_userdata($session_data);
return TRUE;
}
}
?>
From here we can see if the user didn't fill the username and password fields, it will show the error and everything works as expected. The problem is, if the user provides an invalid username or password, the error message won't show.
for the information, I already put $lang['invalid_username'] = 'Invalid username or password'; on the language file.
I am doing this using the HMVC technique. Please help me.
You don't seem to be passing any data to your callback function. Callback functions expect the value of the input field to be passed as a parameter. I don't think you can make this work as a form validation callback be because the authentication method presumably needs to know both the password and the username. At present you're not passing any data into your check_auth method or indeed onwards to your user_model->authentication() method. That is why form_validation is ignoring it.
Rather than calling check_auth as a callback, why not run the form_validation first (this is really what it's for - checking the data is correct and sanitised) and then pass the values from your form to your check_auth function as part of an if statement. You will not be able to use the form_validation set_message method to display errors but I think this is a cleaner approach.
To summarise, use the form_validation to check the data you are receiving is ok and display relevant messages if it is not. Authenticating a user based on that information is a separate procedure that I don't think belongs with validation. Do you see the difference?
from the HMVC wiki page :
When using form validation with MX you will need to extend the CI_Form_validation class as shown below, before assigning the current controller as the $CI variable to the form_validation library. This will allow your callback methods to function properly. (This has been discussed on the CI forums also). ie:
<?php
/** application/libraries/MY_Form_validation **/
class MY_Form_validation extends CI_Form_validation
{
public $CI;
}
<?php
class Xyz extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->form_validation->CI =& $this;
}
}

Create Codeigniter Helper -> undefined method

I created a helper "session_helper.php" in application/helpers/ folder
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('is_login'))
{
function is_login()
{
$CI =& get_instance();
$is_logged_in = $CI->session->userdata('is_logged_in');
if (!isset($is_logged_in) || $is_logged_in != TRUE) {
redirect('login');
}
}
}
And "Configuracion" Controller:
class Configuracion extends CI_Controller {
function __construct()
{
parent::__construct();
$this->is_logged_in();
}
function is_logged_in()
{
$this->load->helper('session');
$this->is_login();
}
}
The problem is when I call the controller "http://localhost/proyect/configuracion" I get the following error:
Fatal error: Call to undefined method Configuracion::is_login() in C:...\application\controllers\configuracion.php on line 15
I read the manual and apparently everything is correct ... what is wrong?
"is_login" is a function, not a method. Just replace $this->is_login(); with is_login();.
Helpers are not methods, they are just simple function calls.
Take a look at helpers in the User Guide: http://codeigniter.com/user_guide/general/helpers.html
Loading a helper:
$this->load->helper('url');
using its helper functions:
<?php echo anchor('blog/comments', 'Click Here');?>
where anchor() is the function that is part of the loaded helper.
Also I would urge you to stay way from calling a helper 'session' make it something more descriptive, as it might become confusing later on. Just a suggestion, its entirely up to you.

Resources