子魚@NET

渤海子魚的BLOG

用户操作
[即时聊天] [发私信] [加为好友]
渤海子魚ID:ffyd2000
14849次访问,排名8277,好友24人,关注者31人。
小二
ffyd2000的文章
原创 18 篇
翻译 0 篇
转载 21 篇
评论 3 篇
渤海子魚的公告
人生就像在风中扫落叶,只是顾着清扫眼前的落叶那是不会顺利的,如果顺着风向堆积落叶的话,那就会变得很轻松了.事物不能光用眼睛看,感觉也是很重要的,仅仅拘泥于眼前的事物,忽略其他的东西也是不行的.抛下邪念,接受现状,这才是所谓的领悟
最近评论
psnccs:WoW Gold
farmer_chs:转贴
SOW觉得文章哪里有问题呢?
SOW:没有搞过PHP不要把别人写的拿来乱说
文章分类
收藏
相册
MY PHOTOS
大牛们
billy
Nosound
刘未鹏|C++的罗浮宫
小新(RSS)
木头
牛蛙
白云
存档
软件项目交易
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
订阅到BlogLines
订阅到Yahoo
订阅到GouGou
订阅到飞鸽
订阅到Rojo
订阅到newsgator
订阅到netvibes

转载 A Practical Tutorial Of Zend Framework(二)收藏

新一篇: A Practical Tutorial Of Zend Framework(三) | 旧一篇: A Practical Tutorial Of Zend Framework(一)

Zend_Controller

Using the controller is pretty intuitive. In fact, I'm writing this tutorial without the luxury of documentation!
Note: Documentation is now available at http://framework.zend.com/manual/zend.controller.html.
I begin with Zend_Controller_Front, a front controller. In order to begin understanding how it works, place the following code in your index.php file:
<?php

include 'Zend.php';

Zend::loadClass('Zend_Controller_Front');

$controller Zend_Controller_Front::getInstance();
$controller->setControllerDirectory('/path/to/controllers');
$controller->dispatch();

?>
If you prefer object chaining, this can instead be written as:
<?php

include 'Zend.php';

Zend::loadClass('Zend_Controller_Front');

$controller Zend_Controller_Front::getInstance()
              ->
setControllerDirectory('/path/to/controllers')
              ->
dispatch();

?>
Now when you make a request for /foo/bar, you get an error. That's good! It lets you know something is happening. The major complaint is that IndexController.php is not found. Before you create this file, it's helpful to understand how the framework expects you to organize things. The framework breaks a request down into parts, and in the case of a request for /foo/bar, foo is the controller, and bar is the action. The default value for each is index. When foo is the controller, the framework looks for a file called FooController.php in the controllers directory. Because this does not exist, the framework falls back to IndexController.php. Not finding either, it reports the error. To continue, create IndexController.php in the controllers directory (which you set with setControllerDirectory()):
<?php

Zend
::loadClass('Zend_Controller_Action');

class 
IndexController extends Zend_Controller_Action 
{
    public function 
indexAction()
    {
        echo 
'IndexController::indexAction()';
    }
}

?>
The IndexController class handles requests for which the controller is index or for which the indicated controller does not exist, as just explained. The indexAction() method handles requests for which the action is index. Remember that index is the default value for both the controller and the action. If you try a request for /, /index, or /index/index, the indexAction() method is executed. (Trailing slashes do not alter this behavior.) A request for any other resource is going to result in an error. There is another useful method to add to IndexController before continuing. The noRouteAction() method is called whenever a request is made for a controller that doesn't exist. For example, a request for /foo/bar executes noRouteAction() if FooController.php does not exist. However, a request for /index/foo still results in an error, because foo is the action, not the controller. Add noRouteAction() to IndexController.php:
<?php

Zend
::loadClass('Zend_Controller_Action');

class 
IndexController extends Zend_Controller_Action 
{
    public function 
indexAction()
    {
        echo 
'IndexController::indexAction()';
    }

    public function 
noRouteAction()
    {
        
$this->_redirect('/');
    }
}

?>
This example uses $this->_redirect('/') to illustrate a possible action to take in noRouteAction(). This causes requests for nonexistent controllers to be redirected to the root document (front page). Now create FooController.php:
<?php

Zend
::loadClass('Zend_Controller_Action');

class 
FooController extends Zend_Controller_Action 
{
    public function 
indexAction()
    {
        echo 
'FooController::indexAction()';
    }

    public function 
barAction()
    {
        echo 
'FooController::barAction()';
    }
}

?>
If you again request /foo/bar, you should see that barAction() is being executed, because bar is the action. Not only can you already support friendly URLs, but you can also do so in a very organized way with just a few lines of code. Cool! You can also create a __call() method to handle requests for undefined actions such as /foo/baz:
<?php

Zend
::loadClass('Zend_Controller_Action');

class 
FooController extends Zend_Controller_Action 
{
    public function 
indexAction()
    {
        echo 
'FooController::indexAction()';
    }

    public function 
barAction()
    {
        echo 
'FooController::barAction()';
    }

    public function 
__call($action$arguments)
    {
        echo 
'FooController:__call()';
    }
}

?>
Now that you can elegantly handle incoming requests with just a few lines of code, you are ready to continue.

发表于 @ 2006年04月18日 17:36:00|评论(loading...)|编辑

新一篇: A Practical Tutorial Of Zend Framework(三) | 旧一篇: A Practical Tutorial Of Zend Framework(一)

评论:没有评论。

发表评论  


登录
Csdn Blog version 3.1a
Copyright © 渤海子魚