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.
Related
public function test() {
echo 'helloooo';
}
this is the controllers/ajax.php
i want to call this function in
views/paymentresponce.php
i tried Ajax::test();
how to call it in the this payment responce
it gives me the error as
Fatal error: Class 'Ajax' not found in C:\xampp\htdocs\handysites\admin\application\views\clienth\paymentrespone.php on line 8
First of all, calling a Controller function from a view is against the principles of MVC.
Views are only for presenting data that is HTML output.
You can call the function in your controller and pass the data to the view.
in views you cannot call controller functions. if you are working with ajax you can call your controller function by url
yoursite.com/controller/function
you can pass controller instance for calling function like
public function test() {
$object['controller']=$this;
$this->load->view('test',$object);
}
View:
$controller->test();
For more :- How to call codeigniter controller function from view
It cannot be Ajax::test();
It should be Ajax->test();
This is not how you call a controller method in CodeIgniter.
You should have something like this for calling a method:
yourController->test(); ?>
In this case, it's:
ajax->test();
If you are accessing this method by clicking a link in your view page, then write like this:
Drink up me hearties, Yo ho!
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.)
I have a custom module with a controller action which performs a certain function,
class Company_CustomModule_ActionController extends Mage_Core_Controller_Front_Action
{
public function doAction()
{
// do something
}
}
I have another controller in the same module (lets say "test"). I would like to call an action within this "test" controller in the above mentioned controller and pass it a parameter like,
class Company_CustomModule_ActionController extends Mage_Core_Controller_Front_Action
{
public function doAction()
{
// do something
// call the index in the "Test" controller
Mage::app()->getFrontController()->getResponse($data)
->setRedirect(Mage::getUrl('company/test'))
->sendResponse();
}
}
My goal is to pass some data from the Action controller to the Test Controller and execute the index action in the Test Controller from within the Action Controller.
Note: in essence, I would like to pass on the POST data received by the Action Controller to the Test Controller.
How would I go about doing this? any guidance would he helpful.
I'm not overly familiar with Magento, but since it's built on the Zend Framework you should be able to use the forward helper and pass your data in the params parameter:
return $this->_forward('test', 'company', $module, $params);
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.