Tag: Zend Framework

Do You Really Need a Framework for Writing PHP?

PHP is a server-side scripting language used all over the world for general purposes. Earlier, PHP used to be the short for Personal Home Page. It is now known as ‘PHP: Hypertext Processor’. It is very helpful for web development and can be blended into HTML too.

Moreover, it is easy to learn. PHP includes many functions without the need for namespace importing. You can decide whether to write object oriented code or not. The syntax is also similar with weakly typed variables.

The simplicity in using PHP sometimes acts against it. Since there are very few coding restrictions, developers tend to write a bad code. The answer to this is definitely to use a framework. There are various PHP Frameworks available today like Zend Framework, CakePHP Framework and CodeIgniter. They provide a strong organization for your application and follow the commonly used MVC pattern.

Some of the other advantages of using a framework are:

  1. MVC structured code:MVC or Model- View- Controller is a well-known design pattern that demarcates the database and business logic from the presentation layer. This results in simple, clean and understandable code.
  2. Maintaining the coding standards:The MVC code is easy to write and also follows the code restrictions. Due to a uniform and standard coding, things are simpler and bugs can be resolved faster. This will save you a lot of worry and it will be easier to find out where the problem lies.
  3. Attractive URLs:Almost all these frameworks make sure that your URLs look attractive and easy to access. Apache takes care of requests for a particular URL, which further invokes the central framework object. The end-users can only see the URL pasted on their browser, and cannot make out that you are using PHP for the same.

  4. Get all the help you need:When you use a PHP framework, you no longer need to manually enter all the validation criteria for forms etc. The only thing you should do is instantiating a new form validator object and instruct it as you wish. It will handle the rest.
  5. Finish coding faster:Frameworks help the developers to write code in lesser time than usual. This leaves them with some extra time for themselves.All said and done, there is always the option of writing your own framework for your application. However, when you have something like CakePHP doing a lot of nifty things for you or a Zend that provides great functionality, you can select one depending on your requirements. So just choose a framework and start coding!
professional-web-apis-with-php-ebay-google-paypal-a Professional Web APIs with PHP: eBay, Google, Paypal, A
US $21.68
Auction Ends: Friday Sep-03-2010 15:10:03 PDT
  | Watch this Item
2200-php-scripts-&-applications-webmaster-mega-kit-plus 2200 PHP Scripts & Applications Webmaster Mega Kit Plus
US $3.99 (0 Bid)
Auction Ends: Friday Sep-03-2010 15:14:46 PDT
  | Watch this Item
php-object-oriented-solutions-by-david-powers PHP Object-Oriented Solutions by David Powers
US $15.59
Auction Ends: Friday Sep-03-2010 16:07:55 PDT
  | Watch this Item
php-nuke-garage-by-don-jones PHP-Nuke Garage by Don Jones
US $8.98
Auction Ends: Friday Sep-03-2010 17:14:56 PDT
  | Watch this Item

Zend Framework vs CakePHP Framework

The Zend Framework, developed by Zend Technologies is an open-source, object oriented web-application framework implemented in PHP 5. It is widely known as ZF and is developed with the purpose of making things easier for PHP developers and supporting best practices at the same time. CakePHP too, is an open-source web application framework used for creating web applications written in PHP. It is developed by Cake Software Foundation, Inc. It is written in PHP and is based on the model of Ruby on Rails.

Zend Framework has easy methods of licensing with the new BSD license and a swift and well-tested code base that your business can rely upon. It makes use of commonly available APIs from well known vendors like Google, Amazon, Yahoo!, Flickr and API providers and catalogers such as StrikeIron and Programmable Web.

ZF offers a simple component library to provide 80 percent of the functionality mostly needed by developers. The rest of the 20 percent can be altered as per your requirements of the business. This can be done since it has a flexible architecture and you do not need any configuration file to get started with. Due to this, training costs are lessened and the product reaches the market sooner. Moreover, Zend Framework also offers:

  • AJAX support through JSON, which offers ease-of-use of the framework.
  • The PHP version of the Lucene Search engine.
  • Simple access to data formats needed in Web 2.0 applications.
  • The ideal destination to utilize and publish web services.
  • Top quality, object-oriented PHP 5 class library.

CakePHP is a rapid development framework for PHP that offers a flexible architecture for development, maintenance and deployment of applications. It uses the usual design patterns like MVC and ORM within the convention over configuration paradigm that helps lower development expenses and does not need the developers to write a lot of code. CakePHP boasts of clients like Mozilla Addons, Scratch by MIT, Yale Daily News, Onion Store and MapMe to name a few.

Just like ZF, CakePHP does not require configuration. It is very simple to use. The company has a user friendly community called #cakephp on IRC that helps new users to begin. It is distributed under the MIT License and promises Best Practices like security, authentication and session handling. It has an object-oriented approach to keep you at ease.

Overall, both these frameworks are good to use as per your requirements. CakePHP is suitable for developers new to MVC and those who require stricter conventions. ZF is a choice for those who need better control over the application design and built-in support for renowned Web services. Both the frameworks are developing fast and the choice is entirely yours.


Zend_Cache is Saving me Money!

Let me tell you a little story.  My latest site I have been working on for some time was written using the Zend Framework.  Since it was launched in Oct 07, it has been growing rapidly enough where I am now starting to feel growing pains.

One morning I went into the site to check some analytics, and my site was displaying a 403 Forbidden error!  I suddenly got a cold nervous sweat down the back of my spine.  Why was it forbidden?  It was fine when I checked it out last night?!  So I tried to login to my FTP account.  No go!  I I thought a hacker has some how blocked me out of my own account. [Exit dramatic build up].

I come to find out that my hosting company revoked my access to the folder that I was using for the domain.  I called them up immediately after I noticed that.  They responded by telling me that on a shared server, they limit their clients to use only 2% of the servers resources before they shut them down.  My site was using 30%. They were then forced to shut the site down without notice, because it was most likely affecting the performance of the other sites running from that shared server.

After some negotiating and explaining to them that I was going to come up with a solution, they temporarily gave me back access to the folder so that I could diagnose further what was going on.  The first thing that I did was looked through the server logs.  Because of my unfortunate neglect, the server log file was freaking enormous bigger than normal.

Nearly all of the errors I was getting were:

[03-Jul-2008 09:41:19] PHP Fatal error:  Uncaught exception 'Zend_Db_Adapter_Exception' with message 'SQLSTATE[42000] [1203] User blank already has more than 'max_user_connections' active connections' in blah blah blah

I’ll admit, I was in a huge hurry to get this project done, so I wasn’t thinking about the long tern effects.  Needless to say, I wasn’t caching my MySQL query results. I know, tisk tisk. Once I discovered this, I added this private method to my classes:


private function loadCache()
{
$frontendOptions = array(
'lifetime' => 7200, // cache lifetime of 2 hours
'automatic_serialization' =>
true);
$backendOptions = array(
'cache_dir' => './cache/' // Directory where to put the cache files
); // getting a Zend_Cache_Core object
return Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
}

Then in each of my classes where I needed to cache my query results, I implemented the above method. Here is an example:


class IndexController extends Zend_Controller_Action {
/**
* Index Action
*
*/
public function indexAction() {
$db = new SomeDB;
$cache = $this->loadCache();
if (!$results = $cache->load('cache_variable')) {
$results = $db->fetchAll('columnName="whatIWant"');
$cache->save($results, 'cache_variable');
}
}
}

It’s pretty much as simple as that.  Once I did this, it dropped the server load tremendously, which now buys me more time from upgrading my hosting package.  About 10 minutes of coding just saved me about $50 a month in hosting fees!  That feels good.

Keep in mind that this is not the best approach to solving this problem.  If you too are using the Zend Framework in your own design.  It was be better to initialize the Zend_Cache in your bootstrap file.  Doing it the way I did above will force you to replicate the code method, loadCache, in all of your classes which I dont’ have to explain to you why this is inefficient, and just plain old bad OOP technique! :P


Email Made Simple with the Zend_Mail Tutorial

I just got finished with an update to one of my sites, the Informative Post, which uses the Zend_Mail component frequently.  Zend_Mail is a component that is apart of the Zend Framework, and if you haven’t ever used it, now is as good of a time as any!

It is my first time working with Zend_Mail and I must say that I am impressed.  This component alone would influence me to use the Zend Framework.  It makes emailing from your site so simple.  You don’t have to deal with the HTTP headers or anything.  Let me show you the code that I used to send a simple email notifying a member that I have received an article that they just submitted.

<?php
include_once 'Zend/Mail.php';

$subject = 'We Have Received Your Article';
$fromEmail = 'admin@gmail.com';
$fromName = 'Administrator';
$toEmail = 'author@email.com';
$toName = 'Author Name';
$message= <<<EMAIL
Hi $toName!

I just wanted to notify you and let you know that I received your article and it will be reviewed shortly.

Regards,
Douglas Brown
Follow Me On Twitter: http://www.twitter.com/dlbrown06
EMAIL;

$mail = new Zend_Mail();
$mail->setSubject($subject);
$mail->setFrom($fromEmail, $fromName);
$mail->addTo($toEmail, $toName);
$mail->setBodyText($message);
$mail->send();
?>

It is as simple as that.  Not too bad is it?  You can also send an HTML email.  It is just as simple.  You would just add setBodyHtml($messageHTML).  $messageHTML would contain the HTML and text that you would like to email.  You should still add the setBodyText method so that if someone who opens up their email doesn’t not want to display the email in HTML, they have the alternative to view it in plain text.

Zend_Mail Tutorial Conclusion

I know that you are thinking that this is a relatively short tutorial, and your right!  The Zend Framework’s Zend_Mail is very simple to use with a minimal amount of code to produce to let you take better advantage of your available time.  You can send an email in 6 lines of code if you would like!  If you feel that I made a mistake in the above code or that you have a better method to use, please let me know, as I would love to learn how to use this component more efficiently.

In my next post, I am planning on showing you how to use the Flickr API to bring your photos to your own site with ease! (continue reading…)


  • BrownPHP Tag Cloud

  • Copyright © 1996-2010 Brown PHP. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress

    Powered by Yahoo! Answers