Adding a hook as for pre_controller gives following error: Unable to locate the specified class: Session.php
hook.php:
$hook['pre_controller'][] = array(
'class' => 'Load_constants',
'function' => 'index',
'filename' => 'Load_constants.php',
'filepath' => 'hooks',
'params' => array()
);
Load_constants.php:
class Load_constants extends CI_Controller {
function __construct(){
parent::__construct();
}
.....
If I remove extended CI_Controller it works well and adding it will throw above error
This issue just appear in CI3 older version works well
Related
This is my database php page
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => '192.168.1.200',
'username' => 'name',
'password' => 'password',
'database' => 'codeignter_tutorial',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
I am unable to load model page from the controller page.
This is the error am getting any help would be appreciated.
Fatal error: Call to undefined method CI_Loader::models() in /var/www/html/bcit-ci-CodeIgniter-3.1.11-0-gb73eb19/bcit-ci-CodeIgniter-b73eb19/application/controllers/Welcome.php on line 16
A PHP Error was encountered
Severity: Error
Message: Call to undefined method CI_Loader::models()
Filename: controllers/Welcome.php
Line Number: 16
Backtrace:
This is my controller page Welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('dashboard');
}
public function __construct()
{
parent::__construct();
$this->load->helper(array("form","url"));
$this->load->models('models');
}
public function login()
{
$this->load->view('loginPage');
}
public function dashboard()
{
$data['name']=$this->input->post('name');
$data['age']=$this->input->post('age');
$this->models->dbdata($data);
}
}
?>
This is my model page called models.php
<?php
class models extends CI_model
{
public function dbdata($inserttodb)
{
$this->db->insert("registration",$inserttodb);
return $var->result_array();
}
}
?>
In model page model's 'm' should be capital
class **M**odels extends CI_model
in controll page
this->load->model('models');
This is how you load model from your controller:
public function __construct()
{
parent::__construct();
$this->load->model('models', 'your_model'); //use 'your_model' to reference your model
}
This is how you called your model:
$this->your_model->dbdata($data);
//'your_model' is from constructor, 'dbdata' is the name of your function in model
Then make sure the method accept the 'data':
public function dbdata($data)
{
//your code goes here
}
I load my models like this
class Admin_user extends CI_Controller {
function __construct()
{
parent::__construct();
CheckLoginIn();
$this->load->model('admin_user_model','model');
}
function index(){
//my function..
}
}
Initially every thing was good...
then i had to use hooks..
my hooks.php file
$hook['pre_controller'] = array(
'class' => 'MyClass',
'function' => 'get_code',
'filename' => 'Myclass.php',
'filepath' => 'hooks'
);
now i get this error.
The model name you are loading is the name of a resource that is already being used: model
however if i set $config['enable_hooks'] = FALSE; everything works fine...
Got the answer.. i had to user post_controller_constructor instead of pre_controller so my hooks file will be
$hook['post_controller_constructor'] = array(
'class' => 'MyClass',
'function' => 'get_code',
'filename' => 'Myclass.php',
'filepath' => 'hooks'
);
I have started to create a controller called User in codeigniter
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library(array( 'encrypt', 'form_validation' ));
}
public function index()
{
}
/*
* Create loginform
* Parameters: void
* Return: html of loginform;
*/
public static function loginform() {
// Login form setup
$loginform_data = array();
$loginform_data['attributes'] = array('id' => 'loginform');
$loginform_data['username'] = array(
'name' => 'username',
'id' => 'username',
'value' => '',
'maxlength' => '100'
);
$loginform_data['pass'] = array(
'name' => 'pass',
'id' => 'pass',
'value' => '',
'maxlength' => '100'
);
$contentdata = array();
$contentdata['loginform'] = $this->load->view('partials/forms/login', $loginform_data, true);
return $contentdata;
}
/*
* Check login username, password from form
* Parameters: void
* Return: void;
*/
public function login() {
$name = $this->input->post('username');
$pass = $this->input->post('pass');
$this->form_validation->set_rules('username', 'Användarnamn', 'required');
$this->form_validation->set_rules('pass', 'Lösenord', 'required');
if ($this->form_validation->run() == false)
{
$this->load->view('home');
}
else
{
$this->load->view('formsuccess');
}
}
}
I can call the user/login - function through the url. But I can't call User::loginform() from another controller. Shouldn't I be able to do that?
Here's what I'm trying: (from my Home-class)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
//Create login and register-forms
$this->load->helper('form');
//Registration form setup
$registerform_data = array();
$registerform_data['attributes'] = array('id' => 'registerform');
$registerform_data['company'] = array(
'name' => 'name-company',
'id' => 'name-company',
'value' => '',
'maxlength' => '100'
);
$registerform_data['orgnr'] = array(
'name' => 'orgnr-company',
'id' => 'orgnr-company',
'value' => '',
'maxlength' => '100'
);
$registerform_data['contact'] = array(
'name' => 'contact-company',
'id' => 'contact-company',
'value' => '',
'maxlength' => '100'
);
$registerform_data['phonecompany'] = array(
'name' => 'phone-company',
'id' => 'phone-company',
'value' => '',
'maxlength' => '100'
);
$registerform_data['emailcompany'] = array(
'name' => 'email-company',
'id' => 'email-company',
'value' => '',
'maxlength' => '100'
);
//What content to pass to view
$contentdata = array();
$contentdata['loginform'] = User::loginform();
$contentdata['registerform'] = $this->load->view('partials/forms/registration', $registerform_data, true);
$this->load->view('home', $contentdata);
}
}
$contentdata['loginform'] = User::loginform(); gives me error:Fatal error: Class 'User' not found in C:\Program...
What am I missing?
While you are extending Home class ,extend to User controller like
require_once(APPPATH.'controllers/user.php');
class Home extends User {
Because you need to extract the function from User class and then it will Inherit the parent class User functions And since login_form is your public function in User controller,you can call this in Home controller now.And no need to use Static here I think so.
There is also an way to do this.Just write the login_form function in an helper and call it on both the controllers then your problem may solve.
Edit: As #IJas said,we need to include the controller file that you are extending.
I've created custom validation library class MY_Form_validation as MY_Form_validation.php in application/libraries as follows.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
public function __construct($rules = array()) {
parent::__construct($rules);
}
public function file_required($file) {
if($file['size']===0) {
$this->set_message('file_required', 'Uploading a file for %s is required.');
return false;
}
return true;
}
}
?>
In my validation function I've included following rules as follows.
public function validate() {
$this->load->library('form_validation');
$config = array(
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'trim|required|xss_clean'
),
array(
'field' => 'display_photo',
'label' => 'Display Photo',
'rules' => 'trim|good|file_required|xss_clean'
),
);
$this->form_validation->set_rules($config);
if ($this->form_validation->run()) {
return true;
}
return false;
}
The core validation rules are working fine but custom rule is not working. So please help me to get the soultion and Its literally wasting my time. The work would be more appreciated.
As far as i understand your function always return true. Because of $file Not $_FILES
public function file_required($file) {
if($_FILES[$file]['size']===0) {
$this->set_message('file_required', 'Uploading a file for %s is required.');
return false;
}
return true;
}
Check the rules in the function validate()
What I think it's incorrect:
$config = array(
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'trim|required|xss_clean'
),
array(
'field' => 'display_photo',
'label' => 'Display Photo',
'rules' => 'trim|good|file_required|xss_clean'
),
);
What I think is correct:
$config = array(
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'trim|required|xss_clean'
),
array(
'field' => 'display_photo',
'label' => 'Display Photo',
'rules' => 'trim|file_required|xss_clean'
),
);
I think good is not a php function, or an internal codeigniter function.
Edit:
What about using:
if ($file['size'] == 0) {
Instead of
if ($file['size'] === 0) {
Using === means the value MUST BE integer 0, but if $file['size'] returns 0 as string the if won't be true, and the function always will return true.
I had the same problem and found the cause while looking in CodeIgniter's source code. It seems the writers thought that if a field didn't have "required", then it would just skip all the rules and always return that the form has validated. See it for yourself from their code:
// If the field is blank, but NOT required, no further tests are necessary
if ( ! in_array('required', $rules) AND is_null($postdata))
However, if you add "callback_" in front of your rule, you can still make it run, for the procedure, look here:
https://www.codeigniter.com/userguide2/libraries/form_validation.html#callbacks
I've just started looking at hooks today not 100% sure what I'm doing wrong but I'm getting an error when I try and use the $ci object in my function.
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: hooks/language.php
Line Number: 12
My hooks file looks like this. It's in the hooks directory in my application folder.
class Language{
var $ci;
public function __construct(){
$this->ci =& get_instance();
}
function get_language(){
echo $this->ci->session->userdata('language');
}
}
I need to get the value in the session to use in my function. Am I not supposed to do it like this?
Thanks you!
In the Base4/5.php file the get_instance() function is written and it is conditionally loaded so it won’t be present until after it is loaded. And that's the reason its giving error.
Just done another Google search and it seems the Hook I was using Pre Controller was before the object was created I've changed the hook and it seems to work fine now.
I used post_controller_constructor for my hook, then the CI worked.
And I had to turn on hooks in the config.
Below is default application/config/hooks.php
// Stores the requested URL, which will sometimes be different than previous url
$hook['post_controller'][] = array(
'class' => 'App_hooks',
'function' => 'save_requested',
'filename' => 'App_hooks.php',
'filepath' => 'hooks',
'params' => ''
);
// Allows us to perform good redirects to previous pages.
$hook['post_controller'][] = array(
'class' => 'App_hooks',
'function' => 'prep_redirect',
'filename' => 'App_hooks.php',
'filepath' => 'hooks',
'params' => ''
);
// Maintenance Mode
$hook['post_controller_constructor'][] = array(
'class' => 'App_hooks',
'function' => 'check_site_status',
'filename' => 'App_hooks.php',
'filepath' => 'hooks',
'params' => ''
);
/* End of file hooks.php */
/* Location: ./application/config/hooks.php */
I have changed it to below and it works fine for me
// Stores the requested URL, which will sometimes be different than previous url
$hook['post_controller_constructor'][] = array(
'class' => 'App_hooks',
'function' => 'save_requested',
'filename' => 'App_hooks.php',
'filepath' => 'hooks',
'params' => ''
);
// Allows us to perform good redirects to previous pages.
$hook['post_controller'][] = array(
'class' => 'App_hooks',
'function' => 'prep_redirect',
'filename' => 'App_hooks.php',
'filepath' => 'hooks',
'params' => ''
);
// Maintenance Mode
$hook['post_controller'][] = array(
'class' => 'App_hooks',
'function' => 'check_site_status',
'filename' => 'App_hooks.php',
'filepath' => 'hooks',
'params' => ''
);
/* End of file hooks.php */
/* Location: ./application/config/hooks.php */