2

私はphp5とZend Frameworkの初心者で、Zend Studioを使用しています。多くのドキュメントを読みましたが、Zend のコントローラーの背後にある概念をまだ理解できません。

簡単に説明すると、アカウント処理用の小さな Web アプリケーションを開発しようとしています。デフォルトの 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(); 

ここに私のIndexController.phpがあります

<?php

class IndexController extends Zend_Controller_Action
{

public function init()
{
    /* Initialize action controller here */
}

 public function indexAction()
 {

 }

}

コンセプトが理解できないので、そこにも変更を加えていません。

私のindex.phtml:

<html>
<style>
</style>

<body>
<img alt="" src="http://localhost/Accounts/application/views/scripts/images/logo.png">
<div id="text" >
<h1>Welcome</h1><br><hr>
<h4>Please Log In To View The Main Page</h4></div><br><br><br>
<form action="main/main" method="post"><center><table>
<tr>
<td>User Name  :</td> <td><input type="text" name="uname"/></td>
</tr>
<tr>
<td>Passowrd   :</td> <td><input type="password" name="pwd"/></td>
</tr>
<tr>
<td><center><input type="submit" value="Log In"/></center></td> <td><center><input type="reset" value="Cancel"/></center></td>
</tr>
</table></center></form>
</body>
</html>

**私が指定したことに注意してください

<form action="main/main">

次のページ、「main.phtml」に移動します。しかし、これはうまくいきません。

ここに私のMainController.phpがあります:

<?php
require_once ('library/Zend/Controller/Action.php');

class MainController extends Zend_Controller_Action {

public function init()
{
    /* Initialize action controller here */
}

public function mainAction()
{
    include 'views/scripts/main/main.phtml';
}
}

上記のコントローラーで、指定すると、

include 'views/scripts/main/main.phtml';

そうでなくても、同じように機能します。ブラウザでログインしようとしても何も表示されません。

ログインの条件を指定していないので、これは main.phtml を表示するはずだと思います。

ここにあります:

<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>

私のコードで何が問題になっていますか? なぜこれが機能しないのですか?私が理解する必要があるのは、これらのコントローラーがどのように機能するかです。ビューをどのようにリンクしますか?

4

1 に答える 1

4

まず、アプリケーションはまったく機能しますか? ナビゲートするとインデックスページが表示されhttp://localhost/accounts/public/indexますか、それともエラーが発生しますか? エラーが発生した場合は、PHP/ZF 環境を修正する必要があります。

次に、アプリケーションの仮想ホストを構成して、ナビゲートを容易にすることをお勧めします。http://accounts/mainの代わりに のような URL が表示されますhttp://localhost/accounts/public/main

ページの表示に関する問題は、単に正しい URL に移動していないことが原因である可能性があります。

今、あなたの基本的な質問に.?

コントローラーのコンセプト...

MVC アプリケーション (具体的には Zend Framework) のコントローラーは、一種のデータ トラフィック マネージャーと考えてください。コントローラーは、Web ページ (ビュー) をモデル (データ) に接着するものです。コントローラーは通常、ユーザーからデータを取得し (フォームなど)、そのデータをフィルターして検証し、モデルに渡してさらに処理したり、データベースやその他のストレージに保存したりするために使用されます。他の方法でも機能します。モデルからデータを取得し、ユーザーに提示する準備をします。

以下は、ユーザーのリストを表示するだけの単純なコントローラー アクションの例です。

//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>

ZF のパーテイルは、指定されたビットのデータを使用して同じコードをループできるようにするスクリプトです。この場合、各ユーザー行はこれらの数行のコードで表示されます。foreach ループのいくつかのインスタンスを置き換える傾向があります。

<!-- 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);
    }
}


コントローラーはアプリケーションに応じて多くの役割を担うことができるため、ここでの答えは少し単純化されていると思います。ほとんどのアプリケーションでのコントローラーの主な役割は、データ管理とビューの準備です。

これがあなたの頭を包み込むのがどれほど難しいかを理解しています。私はこれを少し前に自分で経験しました。Rob Allens の ZF 1.x チュートリアルから始めるとよいでしょう。

また、Zend Studio を使い始めるときは、少し注意が必要かもしれません。プロジェクトの作成中に、Zend_Toolがプロジェクトを作成するときに行うこととは異なるいくつかのことを行うようです (この動作はオプションで変更できると思います)。 Zend Studio によって生成されるコードは、チュートリアルとまったく同じではない場合があります。

コード スニペットは Book Pro Zend Framework Techniquesからのものです。

これが何らかの方向性と助けになることを願っています...

PS ZF が正しくインストールされ、構成されていれば、include ステートメントを使用することはほとんどありません。ZF から何かを含める必要がある場合は、構成に問題があります (おそらく php インクルード パス)。

[編集]

500 サーバー エラーを修正するには、次の点を確認してください。

Apache 設定でhttpd.conf、この行 LoadModule rewrite_module modules/mod_rewrite.soがコメント化されていないことを確認してください。

同じ .conf ファイルで、localhost ディレクトリのディレクトリ エントリに次の設定があることを確認します。

<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>

うまくいけば、これで問題が解決します。

于 2012-07-07T09:30:10.073 に答える