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.

转载于:https://my.oschina.net/elinac/blog/698053

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值