Friday, October 19, 2012

Re: Vendors php classes for cake 2.1

Hi,

thx for your instructions.

I just tried it (cake 2.2) and found that the App::import in the constructor of the wapper class does not get executed for some reason.
Actually the constructor doesn't seem to get executed at all?

As soon as I move all code from the constructor into the domfromfile function, the wrapper works.
However, I would prefer using the constructor. Do you have an idea what might be wrong?

Cheers!
Jens




Am Montag, 19. März 2012 12:23:17 UTC+1 schrieb euromark:
For starters
App::import() is the right way to include vendor files

App::uses() should mainly be used with cake and app stuff

The main difference:
import() usually works like include/require and will load this file right away
uses() will only store the information where to find that file and the lazy loading will fetch this file only if needed

I always try to avoid App::import() and vendor loading directly inside the application.
Wrapping those third party classes in a Lib layer has several advantages.
a) you can easily work with App::uses() internally in the app
b) you can override some methods/settings if they don't make sense
c) changes in how those foreign classes are used/called on updates doesn't have to effect your app
d) makes use of the lazyloading from above
e) it is much easier to test

example: simle_html_dom

in /Lib/Utility/HtmlDomLib.php:

class HtmlDomLib {
public function __construct() {
$res = App::import('Vendor', 'SimpleHtmlDom', array('file'=>'simple_html_dom/simple_html_dom.php'));
if ($res === false) {
trigger_error('simplehtmldom lib not found');
}
}

/**
* @param url or path to file content
* @return object Dom
* 2010-11-06 ms
*/
public static function domFromFile($url) {
return file_get_html($url);
}
}

anywhere in your application:

App::uses('HtmlDomLib', 'Utility');
$DomObject = HtmlDomLib::domFromFile($url);

happy coding
mark


Am Sonntag, 18. März 2012 18:58:01 UTC+1 schrieb dariob:
App:import / App:uses ??



Il giorno domenica 18 marzo 2012 10:36:55 UTC+1, dariob ha scritto:
What is for you the best way to implement any external php class to be avaible trough controllers or models?
We truly need to package any php class in a plugin?? And how? (cake 2.1)

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: