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.
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()
}
}
Is it correct to call controller methods and access its attributes through $this variable when inside a view?
Lets say Im running 'index' view from 'Calcs' controller:
Calcs controller:
class Calcs extends Controller{
public function index(){
$this->set('number', 1);
$this->set('number2', 4);
$this->set('number3', 5);
$this->set('number4', 2);
}
public function doComplexStuff($n1, $n2){
return $n1 + $n2;
}
}
View "index.phtml":
// Html ...
echo $this->doComplexStuff($number, $number2); //5
echo $this->doComplexStuff($number3, $number4); //7
Then i have another controller that also uses 'doComplexStuff' from 'Calcs'
class Another extends Controller{
public function randomView(){
$calcsController = ControllerFactory::getController('Calcs');
$myRandomNumber = $calcsController->doComplexStuff(rand(), rand());
$this->set('myRandomNumber', $myRandomNumber);
}
}
The most common approach in the current MVC frameworks is to pass any results from the controller to the view. One of the ways to do that is to assign them to the view instance as local or global variables.
Avoid calling any controller or model methods inside your views. Use views to display data, controllers to control the flow of your application and models to do do the business logic. This is just a general recommendation/principle implemented in the MVC pattern: http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller.
For a example of the approach taken in Laravel framework,go here: http://laravel.com/docs/4.2/responses#views
$view = View::make('greeting')->with('name', 'Steve');
This way your views will contain only the logic to display the data that they have received from the controller that called them.
Controller is layer which handles requests and delegates data to view. The view layer should just display this data. That's why it is called view. So all calculations should be done in controller. Controller then sends data to view and view doesn't do any calculations and handles displaying of data.
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 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.)
Take a moment and consider the following Laravel controller:
class UsersController extends Controller {
public function index()
{
// some index stuff
}
public function ajax()
{
// This is how I like to do CRUD
if(Input::get('action') == "edit"){
return $this->edit(Input::get('id'));
}
if(Input::post('action') == "update"){
return $this->update(Input::all());
}
...
}
private function edit($id)
{
// Let's populate an edit users dialog (popup)
$viewData = array();
// Call edit method of my Users service & send that data to view
// (Note: "with" is a Laravel Helper)
return View::make('users.usersEdit', with(new Users)->edit($id));
// Or should I just have the service take care of the view, like this:
// return with(new Users)->edit($id);
}
}
In the spirit of "skinny controllers, fat services"...(my spin on the slogan)...
If I feel very confident that the edit method of my Users service (not shown here) is always going to need it's corresponding view, should I have the view called from the service (instead of calling the view from my UsersController as shown above)?
Your view is not a requirement for your edit user method, it is the output of the result. Users is domain logic. It should ideally have little to no dependency on Laravel, so you could stick it into another framework with little to no modification.
A controller is the perfect place to return Views. Definitely not in your domain logic. :)