CAKEPHP - How to use a helper function in a controller?

I wrote some function in app_helper file. now i need to call that function in my controller how i can do this in CAKEPHP

Answers


You can't.*

If your functions are so universal as to be used outside of views, create them in bootstrap.php or make a custom library/class in the libs/ directory.


* You can load anything anywhere using App::import('Helper', 'NameOfHelper') or ClassRegistry::init, but you really shouldn't. The point of MVC separation is to keep your app well organized.


App::uses('HtmlHelper', 'View/Helper');
$yourTmpHtmlHelper = new HtmlHelper(new View());

Is finally the version which works with Cakephp 2.3


App::import('Helper', 'Forum.Common');
    $commonHelper = new CommonHelper(new View())

If you want to use some common functions in all of your controllers like the helper does for views, you must use Components http://book.cakephp.org/2.0/en/controllers/components.html

rather using App::import('Helper', 'NameOfHelper'), this keeps the MVC standard correct and your app well organized.


You can use the helper in Controller as bellow

App::uses('YourHelper', 'View/Helper');

class yourController extends AppController {

public function index(){
    $yourHelper = new YourHelper(new View());
    $yourHelper->yourMethod();
  }

}

Need Your Help

data not being exposed in flash builder

xml flex arrays flash-builder

When I run the flex application, I'm expecting to see 25 records from users table of my database in a list component, and all I see is 25 of this: [object User]

CSS - Hover over any element to trigger transitions on all of them

html css css3 css-transitions

I'm creating a website and I'm making the header in a table, where each column is a different section, the plan is the top row is a picture link and the bottom row is the name of that section... al...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.