Friday 26 March 2010

How to setup zend framework?

mysite
    /application
        /controllers
            IndexController.php
        /models
            IndexModel.php
        /forms
            LoginForm.php
        /views
            /scripts
                /index
                    index.phtml
    /libaray
        /Zend
/js
    /css
    /images
    /index.phtm
 
 <?php
define('ROOT_DIR', dirname(__FILE__));

set_include_path('.'
. PATH_SEPARATOR . ROOT_DIR . '/library'
. PATH_SEPARATOR . ROOT_DIR . '/application/models'
. PATH_SEPARATOR . ROOT_DIR . '/application/forms'
. PATH_SEPARATOR . get_include_path()
);

require_once "Zend/Loader.php";
Zend_Loader::registerAutoload();

$frontController = Zend_Controller_Front::getInstance();

$frontController->throwExceptions(true);

$frontController->setControllerDirectory(ROOT_DIR.'/application/controllers');
$frontController->dispatch();
?>
 
 <?php
class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
    }
}
 
 Now if you browse
http://localhost/mysite/

You will see
Hello World 
If you don’t see “Hello world” printed, read the above guide lines again.
As
we have now successfully created directory structure and bootstrap
file, its time to make other necessary configuration for database.

4. Configuration for working with database
How
a web application can be completed without usage of database. Different
web application uses different database. Some uses Mysql, other SQLite,
SQL server and Oracle. One of the nice thing about Zend Framework is
that it support multiple database servers. 
Here we are going to making configuration for Mysql server.
For database configuration, first create config.ini in mysite/application/ and write the following code in it.
 
 [general]
db.adapter = PDO_MYSQL
db.params.host = localhost
db.params.username = root
db.params.password = 
db.params.dbname = zend_framework
 
<?php
define('ROOT_DIR', dirname(__FILE__));

set_include_path('.'
. PATH_SEPARATOR . ROOT_DIR . '/library'
. PATH_SEPARATOR . ROOT_DIR . '/application/models'
. PATH_SEPARATOR . ROOT_DIR . '/application/forms'
. PATH_SEPARATOR . get_include_path()
);

require_once "Zend/Loader.php";
Zend_Loader::registerAutoload();

$config = new Zend_Config_Ini(ROOT_DIR.'/application/config.ini', 'general');
$db = Zend_Db::factory($config->db);
Zend_Db_Table::setDefaultAdapter($db);

$frontController = Zend_Controller_Front::getInstance();
$frontController->throwExceptions(true);
$frontController->setControllerDirectory(ROOT_DIR.'/application/controllers');
$frontController->dispatch();
?> 
 

1 comment: