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.)
Related
Is it possible to execute multiple functions in the same controller with one route. I thought it would be something like this but it doesn't work.
Route::get('getdata','controller#getData', 'controller#getData1', 'controller#getData2');
In the controller are these functions:
getData
getData1
getData2
Or is there a easier way?
In the controller
Add something like this.
class YourController extends Controller {
//...
protected function getAllData() {
//Executes the seperate functions.
$this->getData();
$this->getData1();
$this->getData2();
}
//...
}
This will execute the functions respectively.
Then from your route, you just call YourController#getAllData as the function of the controller.
It does not make sense if multiple controller actions are responsible for a single route. That's not how MVC works. You should have one and only one action for each route, and call every other function you need inside that action.
And remember, for best practices each method of the controllers must contain only the code to respond the request, not business logic, and if you have any other functions which needs to be called, put them in another other classes (layers).
class MyController extends Controller {
public function myAction(MyService $myService) {
$myService->getData();
// not $this->getData()
}
}
I have a public function in model Yii:
public function test() {
echo 'I am working';
}
Now I want to call this function on view Yii. I wolud like to go through via controller instead of directly calling that function on view Yii.
So how can I call the function on view Yii? and what I have to do with controller before I call it on view Yii?
I prefer to put it on your controller, like this
$model_result = MyModel::model()->test();
$this->render('view', array('model_result' => $model_result));
On your view.php
<div class="my_class" >
<?php echo $model_result;?>
</div>
View should not call any functions (unless perhaps formatting helper functions), controller should pass to the view whatever values it needs, including but not limited to a result of function/method call.
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();
}
// ...
}
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();
I have a piece of code in one of my controllers that I use to call the data for each action and subsequently each view. Rather than repeating the piece of code into each action, what is the best way to create a controller wide function in cakePHP? Or what is best practice?
Example controller:
function get_data($location) {
$orders = $this->Post->find('all',array('conditions' => array('Post.field' => $location));
return $orders;
}
//actual view
function index() {
get_data(waiting);
//etc. etc.
}
//actual view
function view_1() {
get_data(view_1);
//etc. etc.
}
The answer seems to be the fat model, skinny controller approach as outlined in this article.
http://www.sanisoft.com/blog/2010/05/31/cakephp-fat-models-and-skinny-controllers/
You can declare a public function custom_function ($data) in the model and access them in the controller by $this->Model->custom_function($data);
you can use some of this actions:
beforFilter: Called before the controller action
afterFilter: Called after the controller action is run and rendered.
beforeRender: Called after the controller action is run, but before the view is rendered.