使用zendframework 1.8搭建一个简单的网站构架

学习zendframework1.8,现在简单搭建一个使用环境。
步骤如下:
1.建目录结构:如图
zendframework��褰�缁���

2.下载zendframework1.8把它解压后把zend目录放到library目录下。

3.配置环境程序文件
3.1: public/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
3.2:  public/index.php
<?php
define('BASE_PATH', realpath(dirname(__FILE__) . '/../'));
define('APPLICATION_PATH', BASE_PATH . '/application');

// Include path
set_include_path(
        BASE_PATH . '/library'
        . PATH_SEPARATOR . get_include_path()
);

// Define application environment
defined('APPLICATION_ENV')
        || define('APPLICATION_ENV',
                            (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
                                                                                 : 'production'));

// Zend_Application
require_once 'Zend/Application.php';

$application = new Zend_Application(
        APPLICATION_ENV,
        APPLICATION_PATH . '/configs/application.ini'
);

$application->bootstrap();
$application->run();
3.3 application/configs/application.ini
[production]

# Debug output
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0

# Include path
includePaths.library = APPLICATION_PATH "/../library"

# Bootstrap
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"

# Front Controller
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.env = APPLICATION_ENV

# Layout
resources.layout.layout = "layout"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"

# Views
resources.view.encoding = "UTF-8"
resources.view.basePath = APPLICATION_PATH "/views/"

# Database
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "myproject"
resources.db.params.password = "myproject"
resources.db.params.dbname = "myproject_production"
resources.db.isDefaultTableAdapter = true

# Session
resources.session.save_path = APPLICATION_PATH "/../data/session"
resources.session.remember_me_seconds = 864000

[testing : production]

# Debug output
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

# Database
resources.db.params.dbname = "myproject_testing"

[development : production]

# Debug output
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

# Database
resources.db.params.dbname = "myproject_development"

3.4 application/Bootstrap.php
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
        protected function _initView()
        {
                // Initialize view
                $view = new Zend_View();
                $view->doctype('XHTML1_STRICT');
                $view->headTitle('My Project');
                $view->env = APPLICATION_ENV;

                // Add it to the ViewRenderer
                $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
                        'ViewRenderer'
                );
                $viewRenderer->setView($view);

                // Return it, so that it can be stored by the bootstrap
                return $view;
        }
}

到现在我们就可以使用我的zendframework框架了。

4.开始一个简单的例子

4.1 Add a layout

application/layouts/scripts/layout.phtml
<?php echo $this->doctype() ?>
<html>
<head>
        <?php echo $this->headTitle() ?>
        <?php echo $this->headLink() ?>
        <?php echo $this->headStyle() ?>
        <?php echo $this->headScript() ?>
</head>
<body>
        <?php echo $this->layout()->content ?>
</body>
</html>

4.2 Add controllers and views

application/controllers/IndexController.php

这里没有什么特殊的,只是一个控制器。

<?php
class IndexController extends Zend_Controller_Action
{
        public function indexAction()
        {
        }
}

现在要给这个控制器增加一个view,也就是控制器下面的action.

views/scripts/index/index.phtml
<h1 id="welcome-to-the-zend-framework">Welcome to the Zend Framework!</h1>

application/controllers/ErrorController.php
<?php
class ErrorController extends Zend_Controller_Action
{
        public function errorAction()
        {
                $errors = $this->_getParam('error_handler');

                switch ($errors->type) {
                        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
                        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:

                                // 404 error -- controller or action not found
                                $this->getResponse()->setHttpResponseCode(404);
                                $this->view->message = 'Page not found';
                                break;
                        default:
                                // application error
                                $this->getResponse()->setHttpResponseCode(500);
                                $this->view->message = 'Application error';
                                break;
                }

                $this->view->exception = $errors->exception;
                $this->view->request     = $errors->request;
        }
}

views/scripts/error/error.phtml
<h1 id="an-error-occurred">An error occurred</h1>
<h3 id=""><?php echo $this->message ?></h3>

<? if ('development' == $this->env): ?>

        <h4 id="exception-information">Exception information:</h4>
        <p>
            <strong>Message:</strong> <?php echo $this->exception->getMessage(); ?>
        </p>

        <h4 id="stack-trace">Stack trace:</h4>
        <pre><?php echo $this->exception->getTraceAsString() ?></pre>

        <h4 id="request-parameters">Request Parameters:</h4>
        <? var_dump($this->request->getParams()); ?>

<? endif; ?>

到这里一个简单的网站就搭建好了。



你可能感兴趣的:(职场,休闲,zendframework,Bootstrap.php,application.ini)