Calling function in view of cakephp

16,337

Solution 1

please try this for west:

<?php
// controller name like app,users
// action name like getdata should be in controller
// and you can send parameter also
$output = $this->requestAction('controllerName/actionName/'.$parameter);
?>

Solution 2

There are multiple approaches to this. What I cannot tell from your description is exactly what you are looking for. If it is simply to create an array of items that is accessible in your views, I would put it in app_controller.php

var $teams = array('team1', 'team2', 'team3');

beforeFilter() {
   $this->set('teams', $this->teams);
}

Then in your view, you can access the array by the variable: $teams

If you only want to call teams on certain views, it may not be a good idea to set this variable for EVERYTHING. You can get around it by setting up a function in app controller.

function get_teams_array() {
   $teams = array('team1', 'team2', 'team3');
   return $teams;
}

Then put together an element that will call this function: views/elements/team.ctp

<?php
$teams = $this->requestAction(
             array('controller' => 'app', 'action' => 'teams'),
             array('return')
          );

/** process team array here as if it were in the view **/
?>

Then you can just call the element from your view:

<?php echo $this->element('team'); ?>
Share:
16,337
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I have one team array and want that team name every where to show team name.It is possible to built a global function which can return team name and I call that function from my view means ctp file.