A Practical Tutorial Of Zend Framework(二)
2016-06-20 00:00
701 查看
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.phpfile:
<?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.phpis 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,
foois the controller, and
baris the action. The default value for each is
index. When
foois the controller, the framework looks for a file called
FooController.phpin the
controllersdirectory. Because this does not exist, the framework falls back to
IndexController.php. Not finding either, it reports the error. To continue, create
IndexController.phpin the
controllersdirectory (which you set with
setControllerDirectory()):
<?php Zend::loadClass('Zend_Controller_Action'); class IndexController extends Zend_Controller_Action { public function indexAction() { echo 'IndexController::indexAction()'; } } ?>
The
IndexControllerclass handles requests for which the controller is
indexor for which the indicated controller does not exist, as just explained. The
indexAction()method handles requests for which the action is
index. Remember that
indexis 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
IndexControllerbefore continuing. The
noRouteAction()method is called whenever a request is made for a controller that doesn't exist. For example, a request for
/foo/barexecutes
noRouteAction()if
FooController.phpdoes not exist. However, a request for
/index/foostill results in an error, because
foois 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
baris 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.
相关文章推荐
- A Practical Tutorial Of Zend Framework(五)
- A Practical Tutorial Of Zend Framework(一)
- php 5.2.6释出
- INTEGRATING SMARTY WITH THE ZEND FRAMEWORK
- PHP7新特性 What will be in PHP 7/PHPNG
- PHP导出数据库方法
- PHP导出数据库方法
- PHP将uncode转utf8,一行代码解决问题
- PHP将uncode转utf8,一行代码解决问题
- PHP导出数据库方法
- PHP将uncode转utf8,一行代码解决问题
- PHP导出数据库方法
- PHP将uncode转utf8,一行代码解决问题
- Thinkphp开源框架如何使用?
- PHP的安全配置
- VS.PHP 在Visual Studio 下的 PHP 开发 IDE 工具
- 十款PHP开发框架横向比较
- VS.PHP 在Visual Studio 下的 PHP 开发 IDE 工具
- PHP 通过curl post数据到 golang 的webserver
- vsftp简单安装