I got a problem on create a function that calling a function (index function). For ease of understanding let me explain it:
class My_Controller extends CI_Controller{
public function index(){
echo 'This is index function';
}
public function calling_index(){
// doing what's inside the index() function
}
}
Any suggestion ?
There won't be an issue in using
$this->index()
as the controller is simply a class
Related
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');
}
I am trying to call a function Commanfuntion in controller Return_test which is located in the Assign_test controller.
Example:
class Assign_test extends REST_Controller
{
function __construct()
{
parent::__construct();
}
function Commanfuntion()
{
//code
}
}
class Return_test extends REST_Controller
{
function __construct()
{
parent::__construct();
}
function Return()
{
//here I want to call Commanfuntion located in class Assign_test
}
}
As Codeigniter follows the MVC pattern you can't call a controller function in another controller. However you can do this by using a function helper and call the function from function helper in any controller.
I know this is MVC structure incompatible but i need to use this technique:
I have a controller and a model.
I'm calling a function in model from controller.
Model called function calles controllers another function. (This is what generates error).
Example below:
Controller:
public function B($ret=false) {
if(!$ret)$this->Model_model->M($ret);
else echo 'ok';
}
Model:
public function M($ret=false) {
$this->N($ret);
}
private function N($ret=false) {
$this->Controller->B(!$ret); //i can't find how can i call this
}
My first trigger function is:
$this->Controller->B(false);
I've moved code in function B of controller to the Model completely and now everything is going in model itself. B function calling another B function in model and at last step model isn't need to call controller; it is calling B function in Model:
Controller:
public function B($ret=false) {
$this->Model_model->B($ret);
}
Model:
public function B($ret){
if(!$ret)$this->M($ret);
else echo 'ok';
}
private function M($ret=false) {
$this->N($ret);
}
private function N($ret=false) {
$this-B(!$ret);
}
My first trigger function is:
$this->B(false);
And my controller is still has short code.
I have the following controller structure:
with the following code:
MAIN CONTROLLER:
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load_defaults();
}
public function load_defaults() {
}
}
CHILD CONTROLLER 1:
class Child1 extends MY_Controller {
public function __construct() {
parent::__construct();
$this->main();
}
public function main() {
echo "function in Child Controller 1";
}
}
CHILD CONTROLLER 2:
class Child2 extends MY_Controller {
public function __construct() {
parent::__construct();
$this->main();
}
public function main() {
echo "function in Child Controller 2";
}
}
My question: How do I call a function located in Child1 controller, from the Child2 controller?
If you have to call a controller from another controller, you're doing it wrong. Controllers are there to accept URI requests from the client.
Please try to revisit the problem and see if you can move the common logic to MY_Controller - it will be accessible because all other controllers extending it.
Also a model will be good place to have common functions that will be called from controllers.
I think what you are looking for is redirect. Considering that by CI convention each controller function should be an endpoint for the user, if you want to access a function from another controller then what you are really trying to do is redirect them to that controller.
function someEndPoint(){
$this->load->helper('url');
redirect('someOtherEndPoint', 'refresh');
}
i am trying to use $admin->function() instead $this->admin_model->function()
when i tried to declare a variable $admin=new Admin_model; in constructor and use it in other functions it gives error..
my code is given below, i don't know much about OOP concept, somebody please help.
class Admin extends CI_Controller {
var $admin;
function __construct() {
parent::__construct();
$this->load->model('admin_model');
$admin=new Admin_model;
}
public function index($value='')
{
if(!$admin->is_admin_logged_in()){
redirect('admin/login?r='.urlencode(current_url()));
}
$data['loggedin']=TRUE;
$data['account']=$this->session->all_userdata();
$this->load->view('pages/admin-home',isset($data)?$data:NULL);
}
}
presently i am using this method
public function login()// this function belongs to the same controller mentioned above
{
$r=isset($_GET['r'])?urldecode($_GET['r']):'admin';
$admin=new Admin_model;
if($admin->is_admin_logged_in()) redirect($r);
}
i don't want declare $admin=new Admin_model; in every single function and want to make the code look good and clean, so don't like to use $this->admin or $this->admin_model either.
You are trying to give an alias to a model (as what I can see)
All you have to do is:
class Admin extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('admin_model', 'admin');
}
public function index($value='')
{
if(!$this->admin->is_admin_logged_in()){
redirect('admin/login?r='.urlencode(current_url()));
}
$data['loggedin']=TRUE;
$data['account']=$this->session->all_userdata();
$this->load->view('pages/admin-home',isset($data)?$data:NULL);
}
}
Notice where the alias of the model is given
$this->load->model('admin_model', 'admin');
And how it is used
if(!$this->admin->is_admin_logged_in()){