Error with Codeigniter HMVC validation callback - php

I am using Codeigniter with HMVC .
When I validate my form, I use the solution to fix the callback problem in HMVC :
1.Extends CI_Form_validation:
<?php
// application/libraries/My_Form_validation.php
class MY_Form_validation extends CI_Form_validation {
public $CI;
}
?>
In my Controller :
class User extends MX_Controller {
function __construct() {
parent :: __construct();
$this->load->library('form_validation');
$this->form_validation->CI = & $this;
}
My problem is: in my localhost, everything is ok: callback validation run normally. But when I upload my project on host, i execute my controller it appears blank. When I comment the line
//$this->form_validation->CI = & $this;
The controller run normally again.
Could you tell me how i fix this problem?
Thanks for any help.

Check your error logs for any fatal errors, also you should extend my form validation this manner
class MY_Form_validation extends CI_Form_validation {
private $CI
function My_Form_validation($config = array()) {
parent::__construct($config);
$this->CI = &get_instance()
}
//example functions
function my_custom_validation(str){ //or override , for example, required
if($this->CI->method_from_instance()){
return true;
}
return false
}
}
and then use form_validation as you normally do, but now you have extra rule my_custom_validation
EDIT: if by any chance and for some wired reason you want to use controller functions in your library (aka $this) pass it as a config, once you load the library e.g. $this->load->library('form_validation', array(('ci'=>$this) //but i don't see why would you do that.

Related

how to create a contruct() in codeigniter4 controller?

class Home extends BaseController
{
var $cache;
public function __contruct()
{
parent::__construct();
$this->cache = \Config\Services::cache();
}
public function reset()
{
$this->cache->clean();
}
}
assume i have create a web cache.
the idea is i dont want to keep assign the same $cache in every method.
So contructor should do the job.
but when i load the page, it shows -> Undefined variable: cache
how to fix my code/this issue?
class Home extends \CodeIgniter\Controller
{
public function __construct(...$params)
{
parent::__construct(...$params);
// Your own constructor code
}
}
looks like codeigniter have already created a constructor method in the BaseController.
Therefore, i just load the function that i wish to preload/initialized in that BaseController.php.
Fixed.

Codeigniter/PHP include re-useable functions in controller

I have this function that is constantly being reused in my controllers. I have decided to move it into a file that can be referenced all the time. This is my file structure
controllers
|Generic
|Users
|get_all_languages.php
|Users
|Lang
|Lang.php
I want to reference get_all_languages which contains
<?php
function get_all_languages(){
$this->curl->create(GetAllLanguages);
$this->curl->http_login(REST_KEY_ID,REST_KEY_PASSWORD);
return json_decode($this->curl->execute(),true);
}
So far, I have tried including it in the top of my file like:
<?php
include __DIR__.'/../../Generic/Users/get_all_languages.php';
class Lang extends CI_Controller{
However, when I try to use that function like $this->get_all_languages();, an error occurs saying Call to undefined method Lang::get_all_languages()
I have also tried to include it after the __contruct but it doesn't allow me to compile.
I hope someone can let me know how I can reference that function.
Thank you.
You can use library or helper of codeigniter.
And you can load them automatically in application/config/autoload.php.(Reference it)
If you want the specific controller, you can use it in construct of controller using $this->load->library() or $this->load->helper().
For example:
class A extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('libraryname');
$this->load->helper('helpername');
}
public function index() {...}
...
}
...
Updated
application/helpers/global_lang_helper.php
<?php
function get_all_languages(){
$CI = &get_instance();
$CI->load->library('curl');
$CI->curl->create(GetAllLanguages);
$CI->curl->http_login(REST_KEY_ID,REST_KEY_PASSWORD);
return json_decode($CI->curl->execute(),true);
}
At your controller...
public function __construct()
{
parent::__construct();
$this->load->helper('global_lang');
}

How to extend CodeIgniter's library?

So I have followed every rule there is on CI docs page but still get error
Call to undefined method CI_Form_validation::clear_field_data()
I placed the file MY_Form_validation.php in application/core directory:
class MY_Form_validation extends CI_Form_validation {
public function __construct()
{
parent::__construct();
}
public function clear_field_data() {
$this->_field_data = array();
return $this;
}
}
and then in my controller I load class as usual:
$this->load->library('form_validation');
and use my new method:
$this->form_validation->clear_field_data();
$this->load->view('product/create', $data);
Which are the mistakes in my attempt to extend the library?
Your own libraries must be in the folder: application/libraries
$this->load->library('form_validation');
$this->form_validation->clear_field_data();
$this->load->view('product/create', $data);
You're loading the wrong library? Your lib should also go in application/libraries.
$this->load->library('MY_Form_validation');
$this->MY_Form_validation->clear_field_data();
$this->load->view('product/create', $data);

What is wrong with this CodeIgniter code?

I am trying to port an application's few functions from a CodeIgniter application to another existing CodeIgniter application. Both applications on themselves are working very well but when I added this thing it gives the following error:
Fatal error: Call to a member function order_by() on null in …\application\core\MY_Model.php on line 7
In this question I have removed parts unrelated to the error to simplify code.
//MY_Model.php model file
<?php
class MY_Model extends CI_Model {
protected $_order_by = '';
public function get(){
$this->db->order_by($this->_order_by);
}
}
//article_m.php model file
<?php
class Article_m extends MY_Model
{
protected $_order_by = 'pubdate desc, id desc';
}
//frontend.php controller file
<?php
class Frontend extends MY_Controller
{
function __construct()
{
$this->load->model('article_m');
}
function index()
{
$this->article_m->get();
}
}
Please help. Thank you!
whenever calling any $this->db ... you have to make sure to load your database library. Check in application\config\autoload.php for the following:
$autoload['libraries'] = array('database');

Codeigniter extends core class

I want to have class that checks login on all controllers that I specified.
Codeigniter version is 2.1.0 and I have php 5.3.10
Hier is how I would set it up:
I look at https://www.codeigniter.com/user_guide/general/core_classes.html
and I set it up like this:
in the /application/core/MY_Main.php
class MY_Main extends CI_Controller {
function __construct()
{
parent::__construct();
}
}
In my controller I have welcome.php
?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends MY_Main {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('welcome_message');
}
}
So if I place login check in MY-Main it should get work, but I cant get it work anyone???
I needed to add the following code to /application/config/config.php before I got the extenson of core classes working as described in the CI manual.
Code taken from here http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
#include_once( APPPATH . 'core/'. $class . EXT );
}
}
You logic is correct, that should work. It's exactly what I do on all my codeigniter sites. My code is a bit more complex as my login check is being called from a library (so I have to call $CI =& get_instance(); and then $CI in place of $this) but something like below should work for you. logged_in is just a name given to an item of session data set when the user logs in.
class MY_Main extends CI_Controller {
function __construct()
{
parent::__construct();
$session_data = $this->session->all_userdata();
if(!isset($session_data['logged_in']))
redirect('/login');
}
}
In regards to your comment above (http 500), not really sure what's going on there. The code you have pasted shouldnt be throwing errors like that so something else is probably going on. Try turning on codeigniters built in logging functionality.
http://codeigniter.com/user_guide/general/errors.html
You should create a library class and put it inside your library folder and load it as auto_load or inside your controllers.
create functions inside your library for example:
/**
*
* #return boolean check if a user is logged in or not
*/
function notLogin()
{
if (!$this->is_logged_in()){
//echo "pelase <a href='login'><b>login</b></a> to continue ";
redirect('home/login','refresh'); exit;
}
return true;
}
and call it inside your controller constructor or any functions you want like this:
class Main extends CI_Controller
{
private $POST = array();
private $ci_form;
function __construct()
{
parent::__construct();
//check if user is logged in or not
$this->m_auth->notLogin();
$this->load->library('form_validation');
$this->load->library('ajax_pagination');
}
}
It some time happens because of database connection.
Please check if your database :
has been selected by turning-on error reporting from your Cpanel error log.
user has been added to your database.

Categories