Archive for June, 2010
Yii Framework -Final Decision
2
I never felt comfortable with external libraries, those created by other people. Always, since my old times with Visual Basic, have created my own and until now, everything I created was good enough but… Suddenly, after I found that marvellous library called RedBean, I found my self re-programming all my libraries and end up creating a controller and a base model class for a sort of MVC library – I will post it one of these days so you guys can look at it and find out how easy is to implement that incredible library is (thanks Gabor de Mooij!) on your libraries.
But, as a programmer, I started to think that, even though my own libraries speed up my development processes, I want it to be hands on a bigger and more ambitious projects -that happened long time a go but never felt impressed by any library out there. My libraries lacked on scalable development architecture and had to find for a better solution for a much bigger project I am jumping right now. I google around and found interesting offers as exposed on one of my old posts:
Actually, far too many, so I took my time to decide for one to suit all my needs. Checked around and read some good articles about Yii framework and then, telling the truth, I was very impressed with (information taken from http://programmersnotes.info:
- 100% OO architecture. It is really good application design.
- Authentication & roles mechanism
- Caching techniques
- DB access, which is based on PDO
- Active record and relational active record implementation
- Validation – that is really, really nice. To create quite complex register form (check if login is unique, if email is unique, email match with confirmation, passwords match, validate integer/string values, check empty fields and give nice error messages for each field you need only template (view) and model with rules defined. It took me 10-15 mins to do that!)
- Component concept. Just to give an idea, why is it nice – you can define getter and setter methods for properties, you can define read-only properties for components, define and invoke events, attach event handlers and additional features to the class without modifying it, just by attaching additional behaviour to it
- Also, check its graphical benchmarking against other MVC frameworks http://www.yiiframework.com/performance.
Wondering whether DooPHP (claiming is the fastest at the moment) or Yii, I finally decided to use the latest due that DooPHP is quite new and Yii has a huge amount of features and tweaks that, even its documentation is not the best (I just read their Definitive Guide and I think I will have to read to a couple of hundreds articles more to be ‘good’ at it), I think at the end will become the Toolset for highly scalable projects -using its automation tool called Gii, I did the skeleton of a project in less than 30 seconds -duh!.
In addition. last but not least, you can integrate Zend components easily and jQuery is nativelly implemented -and I loooooove jQuery.
For the small ones I still like my own libraries though ![]()
Tweet this!
Speed Up Your Pages With Lazy Load JQuery Plugin
2
I would like to introduce you this simple but very efficient plugin that will help us speed up the downloading time of our web pages. I am talking about mr Mika Tuupola’s Lazy Load JQuery Plugin.
This plugin loads the images of a web page as the user scrolls to their position, that is, images wont load until they are not within the visible viewport margins of the window.
How to use
First we need to insert the following references into our code
<!-- insert a reference to jquery and the jquery.lazyload plugin --> <script src="jquery.js" type="text/javascript"></script> <script src="jquery.lazyload.js" type="text/javascript"></script>
And now this few lines of code in our document.ready function
<script type="text/javascript">
$(function() {
$("img").lazyload({
placeholder : "img/grey.gif",
effect : "fadeIn"
});
});
< /script>
And that’s it! Easy right?
Tweet this!
Uri Class for PHP
0
For years, we developers tend to keep functions that we find useful into our projects, after certain time they become obsolete or you move one to higher level ways of programming. Some of us, can’t get rid of the code that was once collected-created and I thought let’s see if I post one of my silly old classes what can we get out of it? Let’s make it open source hahahaha.
I propose you the following: Here is my small old good set of Uri PHP functions encapsulated into one class, I know is not a great deal but, let’s see what can we all do with it. For every programmer that posts a function, I will write his reference into the class and post his/her site link in this post.
There are certain rules though:
- Functions cannot be greater than 20 lines of code
- Functions cannot depend of far too many external components (maximum one)
- If functions depend of an external component/class, a URL indicating its reference must be provided
- Functions are not to be repetitive, if there is one better written that the ones exposes we will replace it
The Uri Class
Here is my class:
<?php
/**
* @author Antonio Ramirez <contactme@atmyblog.com>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @link http://www.ramirezcobos.com/
*
*
*/
class Uri
{
/**
* Encodes the parameter to hide
* its contents to user
*
* @param string $val
*/
function uriEncode($val)
{
return rawurlencode(base64_encode($val));
}
/**
* Decodes a parameter string that
* was previously encoded with uriEncode
* function
*
* @param string $val
*/
function uriDecode($val){
return base64_decode(rawurldecode($val));
}
/**
* Translates a given url to
* tinyurl version -curl required
*
* @param string $url
* @access public
* @return string $data
*/
function getTinyUrl($url){
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/**
* Translates a given url to
* ToLyUrl version -curl required
*
* @param string $url
* @access public
* @return string $data
*/
function getToLyUrl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://to.ly/api.php?longurl='.urlencode($url));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec ($ch);
curl_close ($ch);
return $data;
}
/**
* Gets Request param
*
* @param string $paramName
* @param string $slash
* @access public
* @return string $param or boolean $false
*/
function getParam($paramName, $slash=false)
{
return (isset($_REQUEST[$paramName])?($slash?addslashes($_REQUEST[$paramName]):$_REQUEST[$paramName]):false);
}
/**
* Serializes Request params
*
* @param string $notThisOne
* @access public
* @return string $query_string
*/
function getParams($notThisOne=false)
{
global $REQUEST_METHOD, $HTTP_GET_VARS, $HTTP_POST_VARS;
$query_string='';
if (strpos($_SERVER['REQUEST_URI'],"?"))
{
list($fullfile, $voided) = explode("?", $_SERVER['REQUEST_URI']);
$cgi = $_SERVER['REQUEST_METHOD'] == 'GET' ? $HTTP_GET_VARS : $HTTP_POST_VARS;
reset ($cgi);
while (list($key, $value) = each($cgi)) {
if ($key != $notThisOne)
$query_string .= "&" . $key . "=" . $value;
}
}
return $query_string;
}
/**
* Makes a Soap Call
*
* @param string $uri
* @param string $command
* @param string $xml
* @access public
* @return string $response
*
* Requires Soap
*/
function makeSoapCall($uri,$command,$xml)
{
try{
ini_set('default_socket_timeout',10); // ten seconds, that is all we wait
$client = new SoapClient($uri,array("connection_timeout"=>10));
$response = $client->__soapCall($command,array(new SoapParam($xml,'Peticion')));
}
catch(Exception $e){
$response = false;
}
return $response;
}
/**
* Parses a given $url for validation
*
* @param string $url
* @access public
* @return $parsed_url
*/
function getParsedUrl($url)
{
$parsed_url = parse_url($url);
if (!isset($parsed_url['scheme']) or $parsed_url['scheme'] != 'http'){
echo 'Unsupported URL sheme given, please just use "HTTP".';
exit();
}
if (!isset($parsed_url['host']) or $parsed_url['host'] == ''){
echo 'Invalid URL given!';
exit();
}
$host = $parsed_url['host'];
$host .= (isset($parsed_url['port']) and !empty($parsed_url['port'])) ? ':'.$parsed_url['port'] : '';
$path = (isset($parsed_url['path']) and !empty($parsed_url['path'])) ? $parsed_url['path'] : '/';
$path .= (isset($parsed_url['query']) and !empty($parsed_url['query'])) ? '?'.$parsed_url['query'] : '';
return 'http://' . $host . $path;
}
/**
* Requests a Url and returns its contents
*
* @param string $url
* @access public
* @return string $query_string
* @uses Snoopy class http://sourceforge.net/projects/snoopy/
*/
function getUrlContent($url)
{
App::import('component','snoopy');
$url = Uri::getParsedUrl($url);
$snoopy = new Snoopy();
$snoopy->agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; es-ES; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12';
$snoopy->referer = '';
$results = false;
if($snoopy->fetch($url)){
$results = $snoopy->results;
// empty buffer:
$snoopy->results = '';
}
return $results;
}
}
?>
Tweet this!
RedBean ORM
1
I was breaking my head towards to find a better ORM approach for my own written framework. I used, and was very happy with P.O.R.K., for quite long time but, as most of us, I was looking for something that could improve the speed of my projects. P.O.R.K. configuration is not hard at all, it is compound of a set of classes encapsulated in three files that allowed me to create models in sort of ActiveRecord style and was really, really good when I first started.
Now my projects are bigger, require a lot more of database analysis and those sometimes go up to 30 tables or more. That means that once analysed a project, I had to create those tables in the database, specify their fields and so on… you know.
I found my self into the lazyness mode and I was already thinking that it could be great to create a set of libraries that do not require any configuration files at all and could manipulate my databases in such a way that not only queries for data but also dynamically creates the tables for me if they do not exists. That could save me around one or two days of work I thought…
Before creating anything I ask mr ‘Google’ if there was something around that could match my concept (never reinvent the wheel) and OH MY… I found it, its name is RedBean and I have to say that is one of the greatest libraries that have ever came across.
How it works
Create a database
Include the required files to work with RedBean -one file: redbean.inc.php
include('mypathto/redbean.inc.php");
Access its core:
//Assemble a database connection string (DSN) $dsn = "mysql:host=localhost;dbname=mydatabasename"; //Connect to database and fetch the toolbox; either with password $toolbox = RedBean_Setup::kickstartDev($dsn, $username, $password);
Now, just for this example: creating one Bean
$rb = $toolbox->getRedBean(); // lets create one object that we didnt even declared anywhere$post = $rb->dispense('post'); // set some variables -fields $post->title = 'This is magic'; $post->created = date('Y-m-d'); // save newly created object to database (how come?) $rb->store($post);
If we were to look onto our database, we will find now that a new table is created called ‘post’ and your bean has successfully saved its properties.
Of course, this is a silly example, in my case I am using redbean on a MVC ‘modified’ pattern (I don’t like the ‘View’ part as it is implemented in modern frameworks and for small projects I use my own approach). Please, have a look to its manual, trust me, it is worth a try.
From the deep of my heart, THANKS for such a great job to his developer mr (Gabor) de Mooij. At last, one EASY yet POWERFULL library for our projects that really help us saving developing time.
Tweet this!
Zend Server CE Personal Experience
12
I would like to tell you all about my ‘incredible’ experience with one of the new Zend Tools called ‘Zend Server’.
Have you ever experience a series of ‘impossible things’ that happened one after the other without any comprehension? Well, that happens to me with Zend Server CE.
I am currently running a MacOSX Snow Leopard on a 17 inch MacBook Pro for the development of my projects and I have the built in Apache, PHP 5.3.1, and MySQL 5.1 as a backend DB. I do work with Dreamweaber CS4 to develop the GUI interfaces together with PhotoShop and Fireworks (all from Adobe Creative Studio CS4) and use Eclipse modified for PHP and Javascript developments. To debug my client-side code I do have Firebug and that’s it.
If I tell you the truth everything was working fine, but I wanted to tweak a bit more the performance of my code (we all do that), so I checked the Zend Tool Zend Server CE. Its documentation was quite good and for the comments of the rest of the programmers I thought was worth a try. This is what happened:
- download and install Zend CE Server
- restarted my computer
- started Zend Server
- damn, nothing works here, nothing! my computer suddenly slows down, what is happening?
- looked at the processes, my God! mr Zend Server had five PHP opened at the same time as a service! And also a service controller that doesnt allow me to stop any of the Services!
- how should i stop this? info: http://files.zend.com/help/Zend-Server-Community-Edition/mac_osx_installation.htm
- this info is even worst, where is installed? where all those scripts that the documentation is talking about? on the Applications folder (where is installed) there is nothing as what they are saying
- found it (have to view hidden files in my Mac, done -defaults write com.apple.finder AppleShowAllFiles FALSE) with a full Mac Search (thanks spotlight)
- uninstalling application
- restarted computer
- going to code a bit, going to Applications folder… diablo! my Finder crashes!
- found tones of info, none of them worked -.DStore, .launch Services, .Onyx, etc, reinstall Snow on top, not a fresh install… reboot, Finder still crashes
- building backups (Time Machine is quite old -a week?), whilst in the process… see next
- looked onto Apache XAMPP install (I do not use it anymore), what??!! htpdocs and all mysql databases GONE!
- i rund around my house screaming, yelling, and asking God why this is happening to me thousand times! -dont laugh, you probably did it a couple of times too
- Worked with Data Recovery, FileSavage, and some data recovery programs that didn’t help at all… where are all those files?
- last solution: fresh install Snow Leopard, Carbon Copy Cloner and TimeMachine working together for a scheduled daily backups.
Well, thank you very much Zend Server, you obviously helped me to improve the performance of my code as I had to rebuild all those libraries that weren’t backed up before making my code faster, cleaner and easy to implement and use… woaw, unreal.
Also, thanks Zend Server CE as I reinstall my OS, having only those apps that really are part of my development process and making me buy a 1.5 TB external drive to have all my backups saved daily. If it wasnt for you I wouldn’t bother spending my money on such things. You rock Zend Server!
Tweet this!
Technology Exact Production Costs?
2
Over the past months we have heard right and left about the new comer and so called great tool ‘iPad’. Now, it is the best tool to interact with Internet, to view-share your photos, to blah, blah, blah… Again, the same story. So, marketing wise, Apple is unreal, their designs, their messages, their ‘hey people wait that is something even greater to come’ is remarkable but let me ask you guys, what is the exact point of so much technology nowadays if most of us, mere mortals, dont have a freaking clue on how the hell all those house-work-devices really work? I bet that 90%, if not 100% of us, don’t use all what a machine-device is capable of. I am a programmer and I don’t even use the full potential of my Blackberry, are you doing something I don’t? I guess not.
For me, and this is a humble opinion, so much technology is crap. Filing the gap between phones and/or netbooks and computers? What a great marketing concept but what a waste of time.
Also, let me share with you something I read on the http://www.independent.co.uk/:
The Foxconn factory in the southern Chinese boom town of Shenzhen is so vast that walking around its outer perimeter takes two hours. Its workers turn out components that are supplied to big Western electronics brands including Nokia, Hewlett-Packard and Dell. And it is here that most of the parts for Apple’s iPhone, and the much-awaited iPad, which goes on sale in the UK this week, are manufactured.
Yesterday, Li Hai, a 19-year-old employee of the firm, jumped from the top of the building in Shenzhen to his death. It brought the number of suspected suicides at the factory this year to 10. There have been another two attempted suicides.
All of the deaths have been of youngsters between 18 and 25 years old. Li Hai had only been working at the plant for 42 days. The incidents have prompted intense soul-searching in China, about conditions in its factories and the social cost of breakneck economic development.
Here you go and yes, we can all excuse our selves saying that, they have chosen the job, they decided to finish their lifes… whatever. The truth is that we don’t really know what happened to the minds of those youngsters and in which circumstances they had to work… Maybe they were forced to work for their families survival, we don’t know. The only thing that matters is that we had iPad right in time, isn’t that true mr Steve Jobs?
In 1984, Apple next launched the Macintosh. Its debut was announced by the now famous $1.5 million television commercial “1984″. It was directed by Ridley Scott, aired during the third quarter of Super Bowl XVIII on January 22, 1984 and is now considered a watershed event for Apple’s success and a “masterpiece”.
Mr Steve Jobs, you are not different from IBM.
Tweet this!