Autoload function and get variables in codeIgniter - php

I have this question: how to load function in codeIgniter in every page and get data or variables from it to show in view?
I have many controllers and each one has many function. Each function load master view as a template and load its own view. Master view has dynamic data I push it from database.
So I need in master view to show some data from database automatically. How to do it?

You can use this. change it according to your
function index()
{
$data['list'] = $this->some_model->show_data();
$data1['content'] = $this->load->view('some_view',$data,true);
$this->load->view('template',$data1);
}
content is in your main template where u will pass your data to show in template

I am not sure what exactly you ask but if you want a function to be loaded automatically to any controller
then create a Mycontroller.php in application/core
class Mycontroller extends CI_Controller
{
function __construct()
{
parent::__construct();
// here goes your function
}
}
and then extend every controller you have to Mycontroller instead of CI_Controller

if you want to use your function inside views. use CI Helpers
Codeigniter Helpers
create your helper function
function get_data() {
// and use CI instance
$CI = & get_instance();
// now you can call your models, libraries. etc. in the helper
$data = $CI->your_model->your_model_funcion();
return $data;
}
and you can call your helper function in controllers, views, models. etc...
get_data();

Related

Accessing controller's function

I am using Codeigniter framework to develop a website. I am currently working on home.php view under the view folder. I need to use UserInfo() function which is inside one of the controllers. Any suggestion how to access that function?
class Welcome extends CI_Controller {
public function UserInfo(){
$this->load->model('model_user');
$data['title'] = 'Users';
$data['users'] = $this->model_user->getUser();
$this->load->view('template/users', $data);
}
}
You cant call controller method inside another controller. Its No Way to do it.
You have two way to resolve this issue
If you want to access the function which place inside the
controller, add that into an model. So by loading model you can call
it.
use redirect('welcome/UserInfo') if you just need to call the function
As You want to call controller function in other controller.In codeigniter App folder core folder exists you make a custom core controller and all other controllers extend with your custom controller
In your Custom Core controller
class CustomCore extends CI_Controller
{
/* ---YOUR FUNCTION IN CUSTOMCORE---- */
public function mycorefunc()
{
//Do something
}
}
and your all other controllers extend with custom core
class YourController extends Customcore
{
function controllerfunction()
{
$this->mycorefunc();// Call corefunction
}
}

Global Query Data

There's a part of a code (query) that will be required in all controllers, they will be passed into views for display.
Can I know is there anyway to declare them in just a single file so that I can reference them directly from my view? Without declaring them in each controller's _construct.
I'm using codeigniter3, here's a sample code:
MainController.php
public function index(){
$data['userCampaign'] = $this->Usermodel->getCampaign();
}
Create default controller in your project which extends CI_Controller and your all controller extends new controller and in __construct(); function of your new controller you can add this code.
Declare this function as protected in parent controller class.
No. I don't know there is method like that.
You want call the function in __construct() or you have to declare function in controller and call it back. $this->check_session()

call a function from view

I need to know how I can call a controller function from view,here is the code,there a some few examples but I couldn't understand,
controller
<?php
class Site2 extends CI_Controller{
public function account_details($id){
$this->load->model('bank_account_model');
$data['ac_results'] = $this->bank_account_model->getAccount();
$this->load->view('view_header');
$data['results'] = $this->get_company_model->get_All();
$this->load->view('view_nav',$data);
$this->load->view('bank_account_detais.php');
$this->load->view('view_footer');
}
}
from the view I need to call acccount_detail function with parameters,
It is really best to NOT call a function from a view, but if you insist, you have several options.
1) You can create a helper (as suggested by xd6_), then load the helper before invoking the view(s).
2) You can put the function in the model and pass the model to the view--similar to how you sent $data to the view_nav view.
3) You can call the function before invoking the view and pass the result to the view, again similar to how you send $data.
4) You can add the function NOT as a member of you controller, but still defined in the the same file as your controller. (I'm not claiming this is a good practice, but it can be done.)

Make things available in any view file Laravel

I have a question about making some elements available in any view file
Lets say im building a webshop and on every page i will be having my sidebar, shoppingcart, user greeting in the top.
How can i make these things available in all my view files?
I could make a class, lets say class Frontend
I could do something like this:
class Frontend {
static $me;
public function get(){
if(!self::$me){
self::$me = new self();
}
return self::$me;
}
private function getShoppingCart(){
// do things
}
public function getData(){
return array(
'Username' => User::find(1)->UserName,
'Cart' => $this->getShoppingCart()
);
}
}
Now in my controller i could pass this Frontend object into the view
View::make('file.view')->with(array('data' => Frontend::get()->getData()));
But this way i will end up with a god class containing way too much stuff and in every controller method i would have to pass these data, which is not relevant to the controller method
Is there a way in Laravel that makes specific data available across all view files?
Thanks!
Use share:
View::share('name', 'Steve');
as per http://laravel.com/docs/responses#views
To keep everything clean, every part of the page should be its own *.blade.php file which would be put together using a master template of sorts.
master.blade.php
#yield('includes.sidebar')
#yield('users.greeting')
#yield('store.shoppingcart')
Then you can use view composers so that each time these views are loaded, the data you want is injected into them. I would probably either create a new file which would get autoloaded, or if you have service providers for the separate portions of your app that these views would use, it would also go great in there.
View::composer('users.greeting', function($view)
{
$view->with('user', Auth::user());
});
In this case, it would make the user model available inside your view. This makes it very easy to manage which data gets injected into your views.
You are close with your 'god class' idea.
Setting a $data variable in a basecontroller has helped me with similar issues
class BaseController extends Controller {
protected $data;
public function __construct() {
$this->data['Username'] = User::find(1)->UserName
$this->data['Cart'] = $this->getShoppingCart()
}
}
class Frontend extends BaseController {
function someMethod(){
View::make('file.view', $this->data)
}
}

how can I store $data array for all methods in a controller

I have some data array which I need for all method's in a controller.
$data['project_ongoing_res_limit']=$this->admin_model->show_project_ongoing_residential_limit();
$data['project_ongoing_com_limit']=$this->admin_model->show_project_ongoing_commercial_limit();
$data['project_upcoming_res_limit']=$this->admin_model->show_project_upcoming_residential_limit();
$data['project_upcoming_com_limit']=$this->admin_model->show_project_upcoming_commercial_limit();
$data['project_completed_res_limit']=$this->admin_model->show_project_completed_residential_limit();
$data['project_completed_com_limit']=$this->admin_model->show_project_completed_commercial_limit();
Problem is I cant DRY this. so I have paste this $data array in each method.
I have a view page for this. so when I load this view , I have to
load above $data array each time/method. this is disgusting when controller
methods are too much.
I want 1 piece of this code like constructor. How can I do this.
you can use traits for this.
Define your methods in a trait, and then use the trait in the controllers.
You can make one helper class in which make a function and put your above code in it but make sure you can't access model using $this so, you need to create CI instance and than access it. after that in your controller in construct method you just need to call this function but don't forget to load the helper class and store it in a variable and pass it along with view.
Just create private data variable in your controller class. Than set your data in constructor. Now you can access your data in any method you want.
class Pages extends CI_Controller {
// ...
private $data;
// ...
public function __construct() {
parent::_construct();
$this->data = array();
$this->data['project_ongoing_res_limit']=$this->admin_model->show_project_ongoing_residential_limit();
$this->data['project_ongoing_com_limit']=$this->admin_model->show_project_ongoing_commercial_limit();
$this->data['project_upcoming_res_limit']=$this->admin_model->show_project_upcoming_residential_limit();
$this->data['project_upcoming_com_limit']=$this->admin_model->show_project_upcoming_commercial_limit();
$this->data['project_completed_res_limit']=$this->admin_model->show_project_completed_residential_limit();
$this->data['project_completed_com_limit']=$this->admin_model->show_project_completed_commercial_limit();
}
// ...
}

Categories