1

I have a string manipulation class that I need in views and in controllers also! I saw that cake reuses code in Components and in Helpers for this type of situations which on my opinion breaks the OOP logic (eg. Session->read)!

Instead of doing this I created a vendor class which I imported in a StringsHelper and in a StringsComponent. I then created an identical function which instanciates the Vendor/String class and returns the results from the corresponding function. This is not quite inheritance and still redundant, but if I change code in my class it changes everywhere.

Is there a better way to do this?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
amstegraf
  • 597
  • 1
  • 5
  • 11
  • 2
    dont use vendor clases here (since it is not a third party product), use Libs (APP/Lib) – mark Nov 26 '12 at 16:16
  • why would you care about OOP principles and concepts when you are already using CakePHP ? – tereško Nov 26 '12 at 21:10
  • I am supposed to port an entire app, developed by OOP principles! Which is now supposed to be part of a bigger app developed with cake! – amstegraf Nov 26 '12 at 22:18

2 Answers2

2

You do not need to wrap this kind of class in a Helper or a Component.

You could simply create a class with static methods and put it in APP/Lib like mentioned by Mark.

<?php
class StringTool{

  public static function manipulate($string){
   ...
  }

}

and then use it in whatever class you need, wether in a Component, a Helper, a Model, etc.

<?php
$s2 = StringTool::manipulate($s1);
nIcO
  • 5,001
  • 24
  • 36
  • I want to use it in a controller and in views, thus I have a Component and a **Helper**. If I still have to pass through them and call functions from my class the situation is the same. My main problem is with the view, otherwise I would've put the code in a Component and that was it, but I need a helper too. I understood why should I move in Lib, it is correct, but it still doesn't help me! Is there a way to call it directly in view? – amstegraf Nov 26 '12 at 17:54
  • same answer: you could use `StringTool::manipulate($s1)` directly in your views or controllers as well, without creating any component or helper. Controllers and views are just PHP, so you can call any lib you want in them. – nIcO Nov 26 '12 at 18:35
  • 1
    static methods have nothing to do with OOP. – tereško Nov 26 '12 at 21:11
  • I understand now! I thought you where saying something like create a Helper and call it there, which was basically the same thing I was already doing, but from another place. Now I saw that I can import it in my Controller and it is available for the view also. No OOP here, but I got the needed result and that is keeping my classes clean! – amstegraf Nov 28 '12 at 11:52
0

I asked this same question before. Best place is in app/Libs, where you can put a class with static helper functions that can be used anywhere in your application, including controllers and views.

Import the class using App::import('Lib', 'YourClass')

CakePHP - Where is the best place to put custom utility classes in my app structure?

Community
  • 1
  • 1
BadHorsie
  • 14,135
  • 30
  • 117
  • 191