Sunday, December 28, 2008

Zend Framework Quickstart - MySQL version

As you might noticed, Magento platform is built on top of Zend Framework. Recently released 1.2 version of Magento eCommerce Platform includes Zend Framework 1.7.2. Zend Framework Quickstart is a good place to start getting an idea about using ZF to build applications. I wrote earlier about installing Magento from svn. The next steps include creating a custom module (todo list, a guestbook something simple for an introduction in creating a custom module for Magento) and connecting Magento with 3rd party applications. I have the following structure on my laptop that I use for development: htdocs is in
C:\web\Apache2.2\htdocs
Zend Framework library:
C:\web\Apache2.2\htdocs\ZendFramework-1.7.2\library
and I've set in php.ini:
include_path = ".;c:\php\includes;C:\web\Apache2.2\htdocs\ZendFramework-1.7.2\library"
1. Create a folder and unzip Quickstart sample application in that folder:
C:\web\Apache2.2\htdocs\zfquickstart #in my case
2. Setup application paths Because you will not build a single application with ZF I don't see a good idea in including ZF within each application (that library folder in sample application folder). I keep it outside in htdocs and I've setup in php.ini include_path so I won't need to override include_path using set_include_path... So, if you want to change include_path in your php.ini you can do that. Or you can change index.php:
#set_include_path(APPLICATION_PATH . '/../library' . PATH_SEPARATOR .get_include_path());
set_include_path('C:/web/Apache2.2/htdocs/ZendFramework-1.7.2/library'.PATH_SEPARATOR . get_include_path());
So start your browser and go to: http://localhost/zfquickstart/public/ You will see a success message from IndexController:
Hello, from the Zend Framework MVC!
I am the index controllers's view script.
View file is application/scripts/index/index.phtml Another thing that we need to fix is the base url, because css file for example is included as /css/global.css and we are in /zfquickstart/public/ and the correct path should be /zfquickstart/public/css/global.css There are many ways to solve that, using Zend_Router and so on... In bootstrap.php find:

$frontController->setControllerDirectory(APPLICATION_PATH . '/controllers');
#add this (you can create a piece of code to extract this from url):
$frontController->setBaseUrl('/zfquickstart/public');
#also find:
$view = Zend_Layout::getMvcInstance()->getView();
$view->doctype('XHTML1_STRICT');
#and add this line
$view->baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
and in layouts/scripts/layout.phtml:
headLink()->appendStylesheet($this->baseUrl.'/css/global.css') ?>
And now you have a nice css added to your layout. Now open public/.htaccess and replace:
RewriteRule ^.*$ /index.php [NC,L]
#with
RewriteRule ^.*$ index.php [NC,L]
3. Setting up database for Zend Framework QuickStart application - MySQL version Create database and grant access:
create database zfquickstart;
grant all on zfquickstart.* to 'zfquickstart'@'localhost' identified by 'zfquickstart';
flush privileges;
schema.mysql.sql:
CREATE TABLE `zfquickstart`.`guestbook`(
`id` INT (8) UNSIGNED NOT NULL AUTO_INCREMENT,
`email` VARCHAR (50),
`comment` TEXT,
`created` DATETIME,
PRIMARY KEY(`id`)
) TYPE = MyISAM;
data.mysql.sql

INSERT INTO guestbook (email, comment, created) VALUES
('email.email@zendfoo.com', 'Hello! Hope you enjoy this sample zf application!', NOW());
INSERT INTO guestbook (email, comment, created) VALUES
('foo@bar.com', 'Baz baz baz, baz baz Baz baz baz - baz baz baz.', NOW());
You can run the above sql with phpmyadmin/some mysql gui... or you can create a version for load.mysql.php in scripts folder to load this sql intro database... Now, setting up config. In application/config/app.ini, add this section:
[development]
database.adapter       = "PDO_MYSQL"
database.params.host   = "localhost"
database.params.username   = "zfquickstart"
database.params.password   = "zfquickstart"
database.params.dbname = "zfquickstart"
Note: in bootstrap.php the environment is development:
defined('APPLICATION_ENVIRONMENT')
 or define('APPLICATION_ENVIRONMENT', 'development');
That's it for now... Next episode: Creating a todo list application with Zend Framework

5 comments:

David S said...

Is there a zip archive with the source code for this sample?

shiva said...

SELECT * FROM emp WHERE ename='smith' AND id=520

please tell me how to implement this qury in zend frame work

shiva said...

SELECT * FROM emp WHERE ename='smith' AND id=520

please tell me how to implement this qury in zend frame work

Chis Florinel said...

@shiva see:
http://framework.zend.com/manual/en/zend.db.table.html

elija said...

This has just driven me nuts for hours

[development]
database.adapter = "PDO_MYSQL"
database.params.host = "localhost"
database.params.username = "zfquickstart"
database.params.password = "zfquickstart"
database.params.dbname = "zfquickstart"

needed to be

[development]
resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "localhost"
resources.db.params.username = "zfquickstart"
resources.db.params.password = "zfquickstart"
resources.db.params.dbname = "zfquickstart"

when using the Zend Framework 11.4, hopefully it will save someone else from the frustration.