Posts Tagged php

Feb 2 2010

PHPBenelux 2010 – My 5 cents

Now, that i am back home, i can write down my thoughts on PHP Benelux 2010, realized in Antwerp, Belgium.

I just signed out for the conference, without really knowing anyone, except from the blogs and previous presentations, i landed in Belgiun without any real expectations about what i was going to bring back to Portugal form the conference.

The event organization

The venue was nice enough for the event presentation, the reception of the event was just great, no need to be a long time waiting for the badge and no hiccups when asking for it. The conference rooms where nice, talk 2 room chairs where somehow not so comfortable, but in general everyone was happy from what i could see. The schedule was nicely controlled by the organization and no big delays occurred, it’s the first time that i saw no real delays happen on a conference of this size, cheers for you guys.

The speakers

The speakers were just great, a lot of PHP known names in the list, with a set of great talks to deliver. Just loved all, but Lorna Jane and Cal Evans where just great, i had to deliver this opinion.

The talks

Sometimes had the hard job of choosing what talk to see, when some of them just overlap at same time, but i’m convicted that i made a great choice, so what did i saw ?

1. Opening Keynote: – The PHP Universe by Derick Rethans
2. Dependency Injection in PHP 5.2 and 5.3 by Fabien Potencier
3. Get the most out of Solr search with PHP by Paul Borgermans
4. PHP applications/environments monitoring: APM & Pinba by Patrick Allaert/Davide Mendolia
5. PHPillow & CouchDB & PHP by Kore Nordmann
6. Passing the Joel Test in the PHP World by Lorna Mitchell
7. Generating dynamic PDFs using Zend Framework and JavaBridge by Eric Ritchie
8. Closing Keynote: Open Teams by Cal Evans

I just left my comments on Joind.in, if you’re feeling curious just go on and check the audience comments on each talks. http://joind.in/event/view/110

Some of the presentations are now uploaded into Slideshare, sho go on and check them out: http://www.slideshare.net/tag/phpbnl10 and some photos on Flickr: http://www.flickr.com/photos/tags/phpbnl10/.

In conclusion, the conference was just great, i am extremely happy for making around 1000 miles to go there and meet the people and watch the conference live. It as great to meet Jeremy and Priscilla Coates and quickly meet Lorna Jane.

Congratulations to all the people that put effort on making the conference memorable and see you all soon.

Jan 3 2010

Zend Framework: How-to interact with Google Calendar (Part 1/2)

Zend Framework has a great interface to access Google Data, this is one of the most easiest ways to implement Google Data API using PHP, in this short how-to i will explain how to authenticate, read and write information from/into your Google Calendar, Zend Framework Documentation has great examples on this. On this first part of this how-to i will explain how-to:

  1. Authenticate on Google Calendar
  2. List all your calendars
  3. Create a new Calendar

I will assume that you are already familiarized with the Zend Framework, and the project is already set up, meaning, include path’s and all extra work.

First let’s include the needed packages into our code:

 
<?php
require_once 'Zend/Gdata.php';
require_once 'Zend/Loader.php';
 
//Let's enable autoload, ZF handles this nicely
Zend_Loader::registerAutoload();
?>

In order to communicate with your Google Calendar, you need first to Authenticate, and there are 3 ways you can use to authenticate: ClientAuth, AuthSub and MagicCookie, you can read the details of each on the Zend Framework Documentation page, on my quick how-to i will use the ClientAuth method.

Authenticating on Google Calendar

 
/**
* Please define your Email and Password for Google Accounts access
*/
define('YOUR_EMAIL_FOR_GOOGLE_ACCOUNTS','');
define('YOUR_PASS_FOR_GOOGLE_ACCOUNTS','');
 
require_once 'Zend/Gdata.php';
require_once 'Zend/Loader.php';
 
//Let's enable autoload, ZF handles this nicely
Zend_Loader::registerAutoload();
$myEmail = YOUR_EMAIL_FOR_GOOGLE_ACCOUNTS;
$myPass  = YOUR_PASS_FOR_GOOGLE_ACCOUNTS;
 
// Parameters for ClientAuth authentication
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($myEmail, $myPass, $service);

Now that we have the authenticated client instance, we are able to interact with Google Calendar. The first action that i do, is to verify if the Calendar i want to write on, exists, if not, i create a new calendar.

Listing calendars and creating a new Calendar

/**
* Lets create the Service instance, to interact with Google Data Api
*/
$service = new Zend_Gdata_Calendar($client);
 
$calendarName = 'My Calendar ZF Example';
$useCalendarFeed = FALSE;
 
try {
// Let's get the calendar list from Google calendar API
$listFeed= $service-&gt;getCalendarListFeed();
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e-&gt;getMessage();
}
 
/**
* Need to run through all the array to search for our calendar name
* Calendar Listing Feed is available on $listFeed variable
*/
 
foreach ($listFeed as $calendar) {
if($calendar-&gt;title == $calendarName)
$useCalendarFeed = $calendar-&gt;content-&gt;src;
}
 
/**
* If we have not found the calendar we want to use,
* we need to create a new one
*/
if(FALSE === $useCalendarFeed)
{
$appCal = $service-&gt;newListEntry();
$appCal-&gt;title = $service-&gt;newTitle($calendarName);
$own_cal = "http://www.google.com/calendar/feeds/default/owncalendars/full";
$service-&gt;insertEvent($appCal, $own_cal);
 
/**
* Need to grab the Google Calendar feed again, with the newly inserted calendar
* We'll need the refreshed feed in order to get the newly added calendar ID in order
* to add the event into the created calendar
*/
try {
$listFeed= $service-&gt;getCalendarListFeed();
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e-&gt;getMessage();
}
 
}

If you go to your Google Calendar account a new calendar named “My Calendar ZF Example” was created.

You can download all the code from my Google Code Repository – Zend Framework Google Calendar Example 1/2.

Hope this helped, on the next how-to i will explain how to create events in your newly created calendar.

Nov 26 2009

Create RESTful Application Using the Zend Framework

Sudheer just release and article on, how to create a RESTful Application using the Zend Framework.

The article enumerates the following steps:

  1. Adding the Zend_Rest_Route in the bootstrap
  2. Creating the Zend_Rest_Controller
  3. Testing the RESTful server using curl

Read the article on Create RESTful Applications Using The Zend Framework.

Mar 7 2009

Starting with Zend Framework – Configuration File and Database Connection

On the first article about the Zend Framework series, i have covered out the Setup of the Development Environment, on this article i will cover the Zend Framework configuration files and the database connection.

I will try to guide you on a brief and short path to start using the database in your Zend Framework project.

First Task – The configuration file

Our first task for today is to create the configurations file, i prefer to use php .ini files to save configuration settings, due to it’s clean and functional organization.

On your /public/config/ folder, create a file named configurations.ini with the following content.

[production]
database.adapter			=	pdo_mysql
database.params.host		=	localhost
database.params.username	=	root
database.params.password	=	xpto
database.params.dbname		=	restaurant
[development : production ]
database.params.host	=	127.0.0.1
database.params.username=	root
database.params.password=	ypto

I am defining my database settings to use PDO_Mysql driver and all the database needed settings, as you can see i am using two sections, the first one named [production] and the second one named [development:production] the : separation means that we are inheriting our development section from um previous production, and we will override some of the definitions. You can read more about this on the Zend Framework Config instructions.


Second Task: Load the configuration settings on your bootstrap file

Now that you have created the configuration file, you need to give some use tho it, lets go to the previously created bootstrap file, on your path /public/bootstrap.php, after the line that contains “Zend_Loader::registerAutoload();“, add the following content:

/**
 * Loading configuration from ini file
 */
$configuration = new Zend_Config_Ini(
    './config/configurations.ini', 
    'development'
);

We are reading the configuration file into a Zend_Config Object, to access the object you could by example do:

echo $config->database->params->host;

 

Third Task: Creating the database handler

Now that we have the configurations needed on a Zend_Config object we are ready to create the database connection. In order to do that, just after the previous code on bootstrap.php file, add the content.

/**
 * Creating the database handler from the loaded ini file
 */
$dbAdapter = Zend_Db::factory($configuration->database);
/**
 * Lets define the newly created handler as our default database handler
 */
Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);
We are creating the database handler and defining the newly created database handler as the default for my front controller.

 Fourth Task: Using the Zend Registry to save the values

Now that we have our application using the database handler, and we are connected to the database, i will save the $configurations values and the $dbAdapter on the Registry for further usage.

/**
 * Lets add the configurations and the database handler to Registry
 * to use in future
 */
$registry = Zend_Registry::getInstance();
$registry->configuration = $configuration;
$registry->dbAdapter     = $dbAdapter;
/**
 * Now that we have the values on the Registry, lets cleanuo the variables
 * from the script scope
 */
unset($dbAdapter, $registry,$configuration);

You can read more about Zend Registry on the framework manual.

On this article, i have approached the Zend Config, Zend_Db and Zend_Registry usage, i are now able to deal with each one of this components, on my next article on this series, i will help you to create a Model extending the Zend_Db_Table.

Mar 4 2009

Starting with Zend Framework – Setup your development environment

This article intends to make you in Zend Framework quickly and painless. I will try to show how can you setup your project in less than 30 minutes.

First Task: Download the library

Download Zend Framework

Second Task: Define your directory structure

This depends mostly from the way you work, on my personal case, i preffer the following directory structure:

  • /
  • /public
    • /config
    • /controllers
    • /models
    • /views
      • /layouts
      • /scripts
  • /shared
    • /Zend

Third Task: Set your project virtual hosts

[sourcecode lang=’php’]<VirtualHost localhost:80>
ServerName localhost
ServerAlias localhost
ServerAdmin [email protected]
DocumentRoot /<DOCUMENT_ROOT>/public
CustomLog /var/log/apache2/localhost-access.log combined
</VirtualHost>[/sourcecode]

Please notice on <DOCUMENT_ROOT>, this should be the folder structure to you newly created directory structure for the project.

Fourth Task: Create the htaccess file to redirect all requests to your bootstrap file

Go to your <DOCUMENT_ROOT>/public/ folder and create the .htaccess file with the following content.

[sourcecode lang=’php’]RewriteEngine on
RewriteBase /
RewriteRule !\.(js|css|ico|gif|jpg|png)$ bootstrap.php[/sourcecode]

 

This way you are defining that all your requests, excluding all the js,css,ico,jpg and jpg file calls will go to bootstrap.php file.

Fifth Task: Create your bootstrap file

Your bootstrap content will look like.

 

[sourcecode lang=’php’]<?php
    /**
     * Bootstrap file
     */
set_include_path(‘.’ . PATH_SEPARATOR . ‘../shared/’);
//include path to zend framework and models

date_default_timezone_set(‘Europe/Lisbon’);
//timezone and time options

/**
 * Zend Front Controller
 */
require_once ‘Zend/Controller/Front.php';

/**
 * Enabling auto loading
 */
Zend_Loader::registerAutoload();

$controller = Zend_Controller_Front::getInstance();

/**
 * Set the path to controllers
 */
$controller->setControllerDirectory(‘./controllers/’)
            ->setBaseUrl(‘/’)
            ->throwExceptions(true);

/**
 * Starting the MVC Layer
 */
Zend_Layout::startMvc(array(“layoutPath”=>”views/layouts”));

$response = $controller->dispatch();   

?>[/sourcecode]

Now that you have your bootstrap file, meaning your request router, you are ready to make your default controller.

Sixth Task: Create the layout file

As you may imagine, you need to create a layout for your project, in this quick how-to we will use the Zend Layout component. On your /public/views/layout/ folder create the file layout.phtml with the following content.

[sourcecode lang=’html’]<?php echo ‘<?xml version=”1.0″ encoding=”UTF-8″?>'; ?>

<?php echo $this->doctype(‘XHTML1_TRANSITIONAL’); ?>

<html xmlns=”http://www,w3,org/1999xhtml”>

    <head>
   
        <?php echo $this->headMeta()->appendHttpEquiv(
            ‘Content-Type’,
            ‘text/html; Charset=UTF-8′
        ); ?>
    </head>

    <body>
     <div id=”header-navigation” style=”float: right”>
        <a href=”<?= $this->url(
            array(‘controller’=>’login’),
            ‘default’,
            true) ?>”>Login</a>
    </div>
        <?php echo $this->layout()->content; ?>   
    </body>

</html>[/sourcecode]

Seventh Task: Your first controller

You need to create the controller that will respond to your first request, we call it the Index Controller. To create it just go to your /public/controllers directory and create a file named IndexController.php with the following content.

[sourcecode lang=’php’]<?php

require_once ‘Zend/Controller/Action.php';

class IndexController extends Zend_Controller_Action {
   
    public function indexAction() {
    }
}
?>[/sourcecode]


Eight Task: Your first view

In order to display something on your controller request handler you need to create the view, we call it the Index View.

Go to you views folder placed under /public/views/scripts/ and create a folder named index, the name of your first controller, create the action name file inside it, as we are handling the action “Index” you should create a file named index.phtml.

[sourcecode lang=’html’]Hello world on INDEX[/sourcecode]

 

In this article i have approached the View and Controller, because this is a Starting with Zend Framework i haven’t approached the Model thematic, i will make an approach to it on next article. There are plenty of contents to explore on the Zend Framework, this article intends to put your on the first step, having your work environment set, the fun begins after this.