Php Can';我不理解Zend框架中MVC的控制器

Php Can';我不理解Zend框架中MVC的控制器,php,zend-framework,zend-studio,Php,Zend Framework,Zend Studio,我是php5和Zend框架的新手,正在使用Zend Studio。我已经看过很多文档,但我仍然不能理解Zend中控制器背后的概念 简而言之,我正在尝试开发一个用于帐户处理的小型web应用程序。我没有对default index.php文件做任何更改。这是: <?php // Define path to application directory defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirn

我是php5和Zend框架的新手,正在使用Zend Studio。我已经看过很多文档,但我仍然不能理解Zend中控制器背后的概念

简而言之,我正在尝试开发一个用于帐户处理的小型web应用程序。我没有对default index.php文件做任何更改。这是:

<?php

// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

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

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
       realpath(APPLICATION_PATH . '/../library'),
       get_include_path(),
)));

/* function _initView()
{
   $view = new Zend_View();
   $view->addScriptPath(APPLICATION_PATH . '/views/scripts/');
} */



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

// Create application, bootstrap, and run
$application = new Zend_Application(
       APPLICATION_ENV,
       APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run(); 
<html xmlns="http://www.w3.org/1999/xhtml" lang = "en">
<style>
</style>

<head>
    <meta name="keywords" content="" /></meta>
    <meta name="description" content="" /></meta>
    <link rel="shortcut icon" href="http://localhost/Accounts/application/views/scripts/images/favicon.ico" >
    <title>Accounts Handling</title>
</head>

<body>
    <div id="header"></div>
    <div id="main">
        <div id="menu">
        <?php include ('C:\wamp\www\Accounts\application\views\scripts\header\header.php');?>
        </div>
        <div id="content">  
            <center><img src="http://localhost/Accounts/application/views/scripts/images/accounts.jpg" alt="image" height=600px width=550px/></center>  
        </div>
        <div>
        <?php include ('C:\wamp\www\Accounts\application\views\scripts\footer\footer.php');?>
        </div>
    </div>
</body>
</html>
不管有没有,效果都一样。 在浏览器上,我尝试登录时未显示任何内容

由于我没有为登录指定任何条件,我认为应该显示main.phtml

这是:

<?php

// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

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

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
       realpath(APPLICATION_PATH . '/../library'),
       get_include_path(),
)));

/* function _initView()
{
   $view = new Zend_View();
   $view->addScriptPath(APPLICATION_PATH . '/views/scripts/');
} */



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

// Create application, bootstrap, and run
$application = new Zend_Application(
       APPLICATION_ENV,
       APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run(); 
<html xmlns="http://www.w3.org/1999/xhtml" lang = "en">
<style>
</style>

<head>
    <meta name="keywords" content="" /></meta>
    <meta name="description" content="" /></meta>
    <link rel="shortcut icon" href="http://localhost/Accounts/application/views/scripts/images/favicon.ico" >
    <title>Accounts Handling</title>
</head>

<body>
    <div id="header"></div>
    <div id="main">
        <div id="menu">
        <?php include ('C:\wamp\www\Accounts\application\views\scripts\header\header.php');?>
        </div>
        <div id="content">  
            <center><img src="http://localhost/Accounts/application/views/scripts/images/accounts.jpg" alt="image" height=600px width=550px/></center>  
        </div>
        <div>
        <?php include ('C:\wamp\www\Accounts\application\views\scripts\footer\footer.php');?>
        </div>
    </div>
</body>
</html>

账务处理

我的代码出了什么问题?为什么这样不行?我需要了解的是这些控制器是如何工作的。它们是如何链接视图的?

首先,您的应用程序能正常工作吗?当您导航到
http://localhost/accounts/public/index
或者您是否收到错误?如果出现错误,则需要修复错误

第二,我建议您为应用程序配置一个,以使导航更容易。您将拥有类似
http://accounts/main
而不是
http://localhost/accounts/public/main

显示页面时出现的问题可能是您没有导航到正确的url

现在谈谈你的基本问题。?


将MVC应用程序(特别是Zend框架)中的控制器想象成一种数据流量管理器。控制器是将网页(视图)粘贴到模型(数据)上的工具。控制器通常用于从用户处获取数据(即表单),过滤和验证该数据,然后将其传递给模型以供进一步处理,或保存在数据库或其他存储器中。它的工作方式也是相反的。从模型中获取数据并准备向用户演示。

以下是仅显示用户列表的简单控制器操作示例:

//navigate to http://myapp/user/list where myapp is the base url, user is the controller
// and list is the action
class UserController extends Zend_Controller_Action{

    public function listAction() { //declare action in camel case
            $currentUsers = Application_Model_DbTable_User::getUsers();//get data from database via model
            if ($currentUsers->count() > 0) {
                $this->view->users = $currentUsers; //send list of users to the view
            } else {
                $this->view->users = NULL;
            }
        }
    }
要显示此用户列表,视图脚本可能如下所示:

<!-- list.phmtl at application/views/scripts/user -->
h2>Current Users</h2>
<?php if ($this->users != null) : ?>
    <table class='spreadsheet' cellpadding='0' cellspacing='0'>
        <tr>
            <th>Links</th>
            <th>Last Name</th>
            <th>First Name</th>
            <th>Username</th>
            <th>Role</th>
            <th>
        </tr>
        <?php echo $this->partialLoop('_user-row.phtml', $this->users); ?>
    </table>
<?php else : ?>
    <p>You do not have any users yet.</p>
<?php endif ?>
<p><a href='/user/create'>Create a new user</a></p>

我意识到这里的答案有点简单,因为控制器可以根据应用程序承担许多角色,它们在大多数应用程序中的主要角色是数据管理和视图准备。

我明白这对你来说有多难,不久前我自己也经历过。一个非常好的起点是。

另外,您可能希望在开始时对Zend Studio有所警惕,因为它在项目创建期间似乎做了一些与创建项目时不同的事情(我认为这种行为可以在选项中更改),因此Zend Studio生成的代码可能与教程中的代码不完全相同。

代码片段来自这本书

我希望这能提供一些指导和帮助

p.S.如果您已正确安装并配置了ZF,则几乎永远不会使用include语句。如果您必须包含来自ZF的任何内容,那么您的配置(可能是您的php包含路径)有问题

[编辑]

要更正500服务器错误,您可以检查以下几项:

在Apache配置文件中,确保这一行
LoadModule rewrite\u module modules/mod\u rewrite。因此
未注释。

在同一.conf文件中,确保本地主机目录的目录条目具有以下设置:

<Directory "C:\Zend\Apache2/htdocs"><--- YOUR DIRECTORY
    ...truncated as the following is the important part
    Options Indexes FollowSymLinks <---IMPORTANT FOLLOWSYMLINKS IS REQUIRED FOR ZF
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All<---IMPORTANT, THIS ALLOWS .HTACCESS TO WORK
    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all
</Directory>

首先,您的应用程序是否工作正常?当您导航到
http://localhost/accounts/public/index
或者您是否收到错误?如果出现错误,则需要修复错误

第二,我建议您为应用程序配置一个,以使导航更容易。您将拥有类似
http://accounts/main
而不是
http://localhost/accounts/public/main

显示页面时出现的问题可能是您没有导航到正确的url

现在谈谈你的基本问题。?


将MVC应用程序(特别是Zend框架)中的控制器想象成一种数据流量管理器。控制器是将网页(视图)粘贴到模型(数据)上的工具。控制器通常用于从用户处获取数据(即表单),过滤和验证该数据,然后将其传递给模型以供进一步处理,或保存在数据库或其他存储器中。它的工作方式也是相反的。从模型中获取数据并准备向用户演示。

以下是仅显示用户列表的简单控制器操作示例:

//navigate to http://myapp/user/list where myapp is the base url, user is the controller
// and list is the action
class UserController extends Zend_Controller_Action{

    public function listAction() { //declare action in camel case
            $currentUsers = Application_Model_DbTable_User::getUsers();//get data from database via model
            if ($currentUsers->count() > 0) {
                $this->view->users = $currentUsers; //send list of users to the view
            } else {
                $this->view->users = NULL;
            }
        }
    }
要显示此用户列表,视图脚本可能如下所示:

<!-- list.phmtl at application/views/scripts/user -->
h2>Current Users</h2>
<?php if ($this->users != null) : ?>
    <table class='spreadsheet' cellpadding='0' cellspacing='0'>
        <tr>
            <th>Links</th>
            <th>Last Name</th>
            <th>First Name</th>
            <th>Username</th>
            <th>Role</th>
            <th>
        </tr>
        <?php echo $this->partialLoop('_user-row.phtml', $this->users); ?>
    </table>
<?php else : ?>
    <p>You do not have any users yet.</p>
<?php endif ?>
<p><a href='/user/create'>Create a new user</a></p>

我意识到这里的答案有点简单,因为控制器可以根据应用程序承担许多角色,它们在大多数应用程序中的主要角色是数据管理和视图准备。

我明白这对你来说有多难,不久前我自己也经历过。一个非常好的起点是。

另外,您可能希望在开始时对Zend Studio有所警惕,因为它在项目创建期间似乎做了一些与创建项目时不同的事情(我认为这种行为可以在选项中更改),因此Zend Studio生成的代码可能与教程中的代码不完全相同。

代码片段来自这本书

我希望这能提供一些指导和帮助

p.S.如果您已正确安装并配置了ZF,则几乎永远不会使用include语句。如果您必须包含来自ZF的任何内容,那么您的配置(可能是您的php包含路径)有问题

<!-- the patial _user_row.phtml --> <tr> <td class="links"> <a href='/page/edit/id/<?php echo $this->id ?>'>Update</a> <a href='/page/delete/id/<?php echo $this->id ?>'>Delete</a> </td> <td><?php echo $this->name ?></td> </tr>

class Application_Model_DbTable_User extends Zend_Db_Table_Abstract
{
    protected $_name = 'users';

 public static function getUsers() {
        $userModel = new self();
        $select = $userModel->select();
        $select->order(array('last_name', 'first_name'));
        return $userModel->fetchAll($select);
    }
}
<Directory "C:\Zend\Apache2/htdocs"><--- YOUR DIRECTORY
    ...truncated as the following is the important part
    Options Indexes FollowSymLinks <---IMPORTANT FOLLOWSYMLINKS IS REQUIRED FOR ZF
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All<---IMPORTANT, THIS ALLOWS .HTACCESS TO WORK
    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all
</Directory>