Is there any way so I can use any helper function in my routes.php?. Whenever I try to make $CI = & get_instance (); It's always give me error CI_controller not found? If I try to load any model that time also give me some error? How can I do this
Related
hi i have a project in laravel 5.4 for some function i have calling the laravel model methods inside the view for my convenient so it is showing me error save() method does not exist so anyone help me is that possible to call the laravel model method inside the view or how to achieve this below is my code
below is my blade code
$pi_amount=new App\PI_Amount;
$pi_amount->invoiceNumber=$fd->invoiceNumber;
$pi_amount->total_goods=$total_goods;
$pi_amount->total_cst=$total_tax;
$pi_amount->total_security=$security_amount;
$pi_amount->freight=$freight;
$pi_amount->total_value=$total_value;
$pi_amount->save();
Make sure:
1. That is the name of the model PI_Amount
2. That it is the model on App\PI_Amount
You can import the model it using at the beginning of the php file:
use App\PI_Amount;
Hi i have create a new static function inside the Controller and call the that static method inside the view and define the process code inside the controller function so my problem resolve.
Below is my controller function
public static function processsAmount($insert_data){
$pi_amount=new PI_Amount;
$pi_amount->invoiceNumber=$insert_data['invoiceNumber'];
$pi_amount->total_goods=$insert_data['total_goods'];
$pi_amount->total_cst=$insert_data['total_cst'];
$pi_amount->total_security=$insert_data['total_security'];
$pi_amount->freight=$insert_data['freight'];
$pi_amount->total_value=$insert_data['total_value'];
$pi_amount->save();
}
and below i have make a call of that static function inside the view like
$security_amount=0;
$insert_data=array('invoiceNumber'=>$fd->invoiceNumber,'total_goods'=>$total_goods,'total_cst'=>$total_tax,'freight'=>$freight,'total_value'=>$total_value,'total_security'=>$security_amount);
echo App\Http\Controllers\PiController::processsAmount($insert_data);
so by using the above concept my problem is resolved and we can use this for further.
Okay, so i have pages controller and user_authenticator controller.
pages controller is like a terminal to my views whilst the user_authenticator controller does the functions that relates to users like registration/logging in.
Whenever i'm done with a function in user_authenticator say for example logging in, how do i load the views via pages controller?
Login->user_auth(controller)->acc_model(model)->user_auth(controller)->view.
to
Login->user_auth->acc_model->pages(controller)->view.
It would be a boon for me if you guys can tell me if what i'm doing is impractical and a better way to do things. Or maybe i should just stick to loading views on the controller i used previously.
EDIT: so i may have forgotten the purpose of my pages controller but i remembered due to a moment of clarity from my foggy and tired mind.
I made a pages controller solely to load views, i guess in a sense, pages won't be loading ALL view but atleast most of the views, for example, if i had links in my views to other views, i would link them via the pages.
For specific functions that need specific controllers i guess i can let them handle loading some views.
Then again, if someone could tell me what i'm doing is a waste of time and should just delete pages controller please tell me so, i'd like to know why.
also if you have any suggestions for further uses of my pages controller thatd be great!
Also regarding session. I have a base controller.
<?php
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function is_logged_in($data){
$session = $this->session->userdata();
if($session['isloggedin']['username'] == ''){
return isset($session);
}else{
return FALSE;}
}
}
?>
How do i make it so that it automatically runs and checks for every controller i load if there are any session set?
Do i have to put it into a constructor? or do i have to call the base controller method from all controllers?
Here is the best solution for you.
You can use Hooks
Step1:
application/config/config.php
$config['enable_hooks'] = TRUE;//enable hook
Step2:
application/config/hooks.php
$is_logged_in= array();
$is_logged_in['class'] = '';
$is_logged_in['function'] = 'is_logged_in';//which function will be executed
$is_logged_in['filename'] = 'is_logged_in.php';//hook file name
$is_logged_in['filepath'] = 'hooks';//path.. default
$is_logged_in['params'] = array();
$hook['post_controller_constructor'][] = $is_logged_in;//here we decare a hook .
//the hook will be executed after CI_Controller construct.
//(also u can execute it at other time , see the CI document)
Step3:
application/hooks/is_logged_in.php //what u decared
<?php
//this function will be called after CI controller construct!
function is_logged_in(){
$ci =& get_instance();//here we get the CI super object
$session = $ci->session->userdata();//get session
if($session['isloggedin']){ //if is logged in = true
$ci->username = 'mike';//do what you want just like in controller.
//but use $ci instead of $this**
}else{
//if not loggedin .do anything you want
redirect('login');//go to login page.
}
}
Step4:application/controller/pages.php
<?php
class pages extends CI_Controller{
function construct ........
function index(){
echo $this->username;//it will output 'Mike', what u declared in hook
}
}
Hope it will help u.
In controllers\home.php there is a class called home and function called index. And right there I have $data['users'] = $this->users->table();
And there is Class Users extends CI_Model within models\users.php.
But CodeIgniter says that Call to a member function table() on a non-object in C:\Program Files (x86)\EasyPHP-12.1\www\HCAWebApp\application\controllers\home.php on line 22 aka where the $data['users'] = $this->users->table(); line is located.
What is wrong with my code? I think it cannot locate the users.php.
You need to load the model prior to calling $this->users via $this->load->model('users').
looking at the error YOU have or did not load the model, load it first like
$this->load->model('users');
OTHERS ..
or if you have a lot of models to load use an array like
$this->load->model(array('users','model1','model2'));
if you like to load it automatically just open config/autoload.php
$autoload['model'] = array('users');
then you can use it now as $data['users'] = $this->users->table();
You could also assign a different name to the model like:
$this->load->model('users','foobar');
then use it as $data['users'] = $this->foobar->table();
I am using codeigniter 2.1.3
I am trying to load a model from the library. Initially my code in the construct in the library looks like this
$this->CI=& get_instance(); $this->CI->load->database('default')
Then in one of my library methods
when I tried the line below it doesnt work
$this->load->model('model_name')
but when I tried this
$this->CI->load->model('model_name','',TRUE)
it works, anyone can explain what the instance of CI refers to and the 2 extra parameters when loading the model? Thanks in advance.
A library is not necessarily a part of the way CodeIgniter works.
It could be a homemade library, to solve a task that you want done in your CI application.
This means that if you want to use any of CI's helpers, models or other libraries, you need to do this through the CI instance. This is achieved by doing this:
public function __construct()
{
$this->CI =& get_instance();
}
By assigning the instance to your librarys member named CI, all CI related helpers, models and libraries can be loaded through $this->CI. By trying to do it only with $this you are only referring to the current library - not the CI instance.
To load your model correctly, in your library, $this->CI->load->model('model_name'); is enough. The second parameter allows you to access your model through a different object name. The third parameter is not necessary for loading models, but allows you to autoload the database driver.
If you want to access your model through the same member:
$respone = $this->CI->model_name->method();
I have very simple code that you should use to load model in library
<?php
class Custom_lib
{
private $_CI;
public function __construct()
{
$this->_CI = & get_instance();
$this->_CI->load->model('Dynamic_Model','dm');
}
function currentSession()
{
$session = $this->_CI->dm->fetchSingleData('id,session','session',array('is_active'=>1));
return $session;
}
}
I hope this code will help you
You can tell the model loading function to auto-connect by passing TRUE (boolean) via the third parameter, and connectivity settings, as defined in your database config file will be used:
$this->load->model('Model_name', '', TRUE);
You can more about this at the end of the page of this below link.
http://ellislab.com/codeigniter/user-guide/general/models.html
I know this has been asked before, and I've looked through every answer posted, but to no avail. Basically, I am trying to extend the Codeigniter form validation library on a CI2+ project, but every single time I get the error:
Fatal error: Call to a member function get_errors_array() on a non-object
Below is my code:
application/core/CB_Form_validation.php
class CB_Form_validation extends CI_Form_validation{
function __construct($rules = array()) {
parent::__construct($rules);
log_message('debug', "CB_Form_validaiton class initialized");
}
public function get_errors_array() {
return $this->_error_array;
}
}
and then in my ajax file I have the construct etc.
public function save(){
if($this->form_validation->run() == FALSE){
}
}
and inside that if statement I have tried the following:
echo $this->form_validation->get_errors_array();
echo $this->cb_form_validation->get_errors_array();
echo get_errors_array();
I have also tried placing the CB_Form_validation.php in application/libraries as well. Just as well, I have also tried this in my construct of my ajax controller
$this->load->library('CB_Form_validation');
$this->load->library('core/CB_Form_validation');
And I have set CB_ as my custom class prefix
Turns out that to fix this, you should do the following:
Move your custom form validation class to the application/libraries folder
You can keep the construct of your custom class how it is
Use $this->form_validation->method() to access the function you would like
As long as your loading the form_validation library, you don't need to perform $this->load->library('custom_class') because CI picks up on your custom prefix anyways
Hope this helps someone