I am new in the CodeIgniter framework and PHP. I am trying to call a method which is in another module's controller. For that I am using:
modules::run('addons/demo');
but it does not work. How can I accomplish this task?
include it and it will work but it is a bad practice to use this. Instead create a library and define the method there and it will be accessible in the whole application where ever you load the library. in the application/library create my_library.php
<?php Class My_library{
function common_method(){
echo 'this is a common method';
}
}
And call it in the controller method
<?php Class test_controller extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
$this->load->library('my_library');
$this->my_library->common_method();
}
}
First just need to load the module
$this->load->module("module_name");
Then call the controller method from the loaded module.
$this->module_name->method_name();
Related
I have this code in the HomeController#index:
$towns = Town::all();
return Redirect::to('home')
->with('towns', $towns);
Is there any way I can tell Laravel to execute that lines of code before the end of methods and controllers I define without me copying and pasting those lines of code in every method?
You don't need to do that, you can just share this data with all views by using the view()->share() method in a service provider:
view()->share('towns', Town::all());
You can also use a view composer for that:
public function compose(View $view)
{
$view->with('towns', Town::all());
}
You can extend all controllers from your basic controller. Use Controller.php on app/Http/Controllers/controller.php or create new one.
Add myThreeLines to base controller.
controller.php:
function myThreeLines(){
$towns = Town::all();
return Redirect::to('home')
->with('towns', $towns);
}
class TestController extend Controller{
function index(){
return $this->myThreeLines();
}
}
I have two controller classes in my codeigniter application, say class A and B.I just want to create an object of class A and access the functions declared in class A from class B.Something like:-
class A extends someclass
{
public function function1(){
$this->load->view('welcome_message');
}
}
}
class B extends someclass2
{
protected $object;
public function __construct()
{
parent::__construct();
$this->objectA = new A();
}
}
}
I want to access the function function1 from class B using the object objectA. How can i do this?
Please help.
Thanks
well actually this is not the proper way in codeigniter. Actually when you have common functions in and you want to use them in 2 or more controllers. The best way is to create base controller in core folder with name of MY_Contoller and extend it from CI_Contoller. Write your common function in MY_Contoller. Now you have to extend all your controllers from MY_Contoller instead of CI_Contoller. You can do the same with Model.
Cross-controller access goes against CI best practice.
Either inherit both controllers from a controller that holds this common functionality (don't forget to prefix the function with '_' so it's inaccessible via url routing) or create a library that contains your re-usable functionality. A helper can also work.
Can we call a controller and parsing any values or data to the controller from a method?
let's say that i have this method,
function loader(){
//some operations to call another controller
}
and from that method i want to call a controller named welcome.php wich is located in /application/controller
i'v tried this but it doesn't work
function loader(){
$open = new Welcome();
}
it says that Class Welcome not found
Sorry for my bad english
At first You have to include the file
include('welcome.php');
Then, create the object.
function loader(){
$open = new welcome();
//if you want to call a method in an object
$open->MyWelcomeMethod();
}
Makesure that your loader controller was extended to welcome controller.
Suppose controller welcome,my_controller are two controller and loader function in B then
class Welcome extends CI_Controller {
function my_fun() {}
}
then you can call my_fun() when you are entended from my_controller like
class My_Controller extends Welcome {
$open = $this->my_fun();
}
I am using CodeIgniter. I want to use my own class to pass as an argument inside controller functions.
Normally, I can put this class in a folder and include it to MY_Controller with its path. But I want to learn if there is a way to do this in CodeIgniter. I can't put it in libraries folder and can't use loader class because it tries to create an instance of an object, but I want to create instance whenever I want. Loader class gives an error if my own class need constructor parameters.
What is the best way to do that?
Which is the best folder to put in it?
This is very easy. Consider this example
<?php
Class Home extends CI_Controller{
public $arg1 = 1;
public $arg2 = 2;
function index($this->$arg1 , $this->$arg2){ //or function index()
//Then inside function
//$vara = $this->$arg1 , $varb = $this->$arg2;
}
}
hi i am using codeigniter , in my controller constructor sometimes i use $this sometimes $this->ci
in two constructors i have use like this
public function __construct()
{
$this->ci =& get_instance();
$this->ci->load->library('form_validation');
$this->ci->load->library('catalog/CatalogManager');
}
function __construct()
{
parent::__construct ();
$this->ci = & get_instance ();
$this->load->library ( 'auth_lib' );
$this->load->library ( 'session' );
}
when passing data to view i use
$this->ci->data and $this->data in above two cases .
neither gives errors , but i am confused , what is the correct use.
please help...........
All controllers extend the main CI_Controller, so calling something like $this->load means accessing the parent method load() inside the parent class CI_Controller.
$this->ci works because with $this->ci = &get_instance() you're calling a reference to the main controller class...again. If you look in the bootstrap file (IIRC. Or the codeigniter.php file) there's the function get_instance(), which does nothing but return (by reference) the instance of the CI_Controller class.
So, basically, calling $this->ci->load and $this->load are the same exact thing, only that the first is unnecessary within a Controller/Model/View because the system is already doing that in the parent class (through the method load).
If you have a look at libraries, for ex., you'll see instead that using $this->ci->method() is necessary, because you need to have available all the methods of the CI_Controller, which is a kind of "super class" that drives the whole framework.
Have a look at the loader class and the CodeIgniter class to grasp how CI internally works.
Agree with the answer above, but actually, load is a variable, not a function. it is a object of the class CI_Loader, when you call $this->load->libray(), in fact it calls the library() function in CI_Loader.
$this is nothing. It just use to store the value. It just like a variable.