URL分析和典型的控制器文件格式 - ZF Dream

URL分析和典型的控制器文件格式

阿克 posted @ 2008年10月23日 06:26 in 自学笔记 with tags 控制器 URL , 2408 阅读

Zend Framework是一个使用统一入口的框架,它的URL并非用来定位访问的文件,而是分析访问者要寻找的控制器(Controller)和动作(Action),并以此为标准找到相应的代码来执行。不过我们之前还说过模块(Module),所以URL基本由这三部分构成。

下面这是个典型的URL:

http://www.example.com/module/controller/action

www.example.com是域名,这个做网站的都懂,不多说了。后面的module作用是告诉index.php,我要找application下名为module的那个模块。这个名称必须和index.php中的$controller->setControllerDirectory对应。假设module这个模块没有在index.php中设置好ControllerDirectory,ZF会报错。

找到module模块后,再找controller。这个文件必须在application/module/controllers目录下呆着,而且它的文件名必须为controllerController.php,注意后面那个Controller是大写开头。如果这个控制器的名字叫index,那这个文件名就是indexController.php。index是默认的控制器,大家的项目中必然非常熟悉这个控制器!

找到控制器文件后,ZF会到这个文件中寻找请求的action动作。按照ZF规则,控制器文件中,必须建立一个类,类名为Module_ControllerController,继承Zend_Controller_Action类。在这个控制器类下,你可以放自己设计好的动作。既然前面请求action这个动作,那这里必须有一个actionAction()的函数,不然ZF也会报错。

<?php

// 导入动作类
require_once 'Zend/Controller/Action.php';

class Module_ControllerController extends Zend_Controller_Action
{
       
    public function actionAction()
    {
        // 做什么写这里
    }
}

接下来,函数中的内容就是负责真正处理的代码。

由于MVC是预设好的,所以控制器文件、控制器类名、动作函数名都必须遵守ZF规则,不然都会报错。module可以是default,这样就不必再URL里体现,写Controller类名时也不必写出来了,controllerController就可以。

通过这样的分析,可以发现,MVC真麻烦!做小网站时没有必要使用这种方式,繁杂的前端控制和分发工作会降低代码效率。经过测试,即使是现实一个简单的“Hello World”,ZF都需要花200ms以上。不过增加动作代码,设计复杂之后,再测试,你会发现代码耗费的时间没有增加许多。

(阿克原创,版权所有)

Avatar_small
deep cleaning dubai 说:
2019年10月14日 13:29

We offer numerous cleaning products like office environment, carpet, chair, curtain, free of moisture cleaning, deep maintenance in Dubai and more. We value customer care to the top level and provides consistent maintenance in Dubai. We offer customized reserved cleaning products in Dubai to your customers.

Avatar_small
part time maids in d 说:
2021年9月28日 16:35

You ought to avoid costs your services over a per hour or so basis, as this might just become pricing the labor - and achieving this will not necessarily cover the skills and also efficiency as soon as you reach an increased level. A tiny research about existing opposition can produce the costs information you should let an individual set the proper price to your cleaning rates. If you would like to work professionally, you need to put a price on the labor and also practice simply by cleaning your home to determine enough time required to execute certain washing jobs.

Avatar_small
maid services dubai 说:
2023年8月28日 16:59

Find references through the maid assistance. Call every single reference and enquire of if they have got had just about any complaints if they propose the assistance. Inquire about whether or not they are still with all the service. Or else, ask precisely why. Look into whether or not the company has any kind guarantee regarding quality. Do your maids get supervisors if so exactly what they to blame for? Request information through the company on whether they provide just about any assurances, along with what his or her current criticism process can be.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee