Tuesday, March 20, 2012

Re: Vendors php classes for cake 2.1

Thank you Mark, very good answer... I will try this night ;)

Il giorno lunedì 19 marzo 2012 12:23:17 UTC+1, euromark ha scritto:
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)

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: