ZF的module不错

  前面使用Zend framework开发的项目不算太复杂,目录结构和controller都相对简单,近期开始的东西涉及到子域名、多子系统,于是重新看了Zf的文档,发现了Controller里面的module功能很不错。

  先规划如下目录结构:
[coolcode lang=”php”]
docroot/
index.php
application/
default/
controllers/
IndexController.php
blog/
controllers/
IndexController.php
models/
views/

[/coolcode]
  然后在index.php里面写入类似下面的代码
[coolcode lang=”php”]
$front->setControllerDirectory(array(
‘default’ => ‘/path/to/application/controllers’,
‘blog’ => ‘/path/to/application/blog/controllers’
));
[/coolcode]

  可以通过 http://host/blog/controller/action 的方法来访问了。

Category: PHP / Zend
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
17 Responses
  1. 800sun says:

    如果一台主机下有两个应用或者网站,比如8zone和toplee。单靠根目录下的一个index.php文件好像就不能处理了。

  2. dzq says:

    1.
    如果
    http://host/blog/controller/action => http://blog.host/controller/action在lighttp上rewrite吗?
    2.
    default/ 是什么意思呢?你用绿色字体强调过,是作为网站公共模块吗?
    ‘default’ => ‘/path/to/application/controllers’
    似乎应该:
    ‘default’ => ‘/path/to/application/default/controllers’

  3. Michael says:

    default是默认的,就是不用写也行。

    另外,如果说是一个index.php处理多个host,可以自己在index.php里面根据 $_SERVER[‘HTTP_HOST’]来判断,然后设置不同的参数即可。

    不过感觉的确有些别扭,因为既然两个host,还是分开两个应用的好。

  4. dzq says:

    用setControllerDirectory处理后,还能做成子域名形式吗?

    比如:http://blog.host.com/

  5. Michael says:

    根据不同的$_SERVER[‘HTTP_HOST’]设定不同的 setControllerDirectory()
    方法比较土,但是能实现。

  6. 南半球 says:

    上周末有幸,能跟Michael大哥一起吃顿晚饭,聆听你的指点
    感觉到很是幸运,受益非浅,这周很忙,但时时回忆你所讲的,受益启发很大。

    也使我能安心好好做技术,经你一点拨,感觉做技术开发的人生一样也会很丰富。
    本来以为在互联网、SP之类的公司做技术很没有层次感,现在有了更深入的认识。

    感谢你的能开阔无私的跟后来的人分享你的经验、技术。
    以后一定向你学习,在技术方面提高自己,深入下去,在做人做事态度上胸襟开阔

  7. fenglan says:

    看来站长的确是个人品不错的人,希望有机会也能认识和请教。

  8. Michael says:

    哈哈,南半球兄弟实在过奖了,能给朋友一些帮助是我非常乐意的。俗话说给人方便就是给自己方便,希望有机会再次聚聚或者一起打球运动。

  9. dzq says:

    Michael,
    这种文件结构,公用的类怎么放置合适呢?比如:分页类、检测用户登录等。

    你基于ZF做的产品,目前性能和效率方面怎么样?希望再看到你的分析。

  10. Michael says:

    公用类放在include_path跟Zend目录平级的地方,或者任何其他目录,不一定全部都要放到models下,其实都是灵活的,并不是ZF的controller就限制了你不能使用PHP的其他特性和功能了。

  11. dzq says:

    没错,要灵活使用。
    目前公用类就是放到跟zend目录平级的地方。

    另一问题:
    你是怎么处理找不到页面/程序有问题这一类的错误处理呢?
    如果要把它们定向到一个页面怎么处理呢?

    谢谢!

  12. dzq says:

    现在用户贡献内容的成本提高了,我发现写留言提交后都要到另一个页面填验证码。
    并且填写正确的验证码后就到了一个不知所云的页面,想看看是否发送成功,只能重新www.toplee.com

  13. Michael says:

    [Comment ID #15915 Will Be Quoted Here]

    错误捕获的方法一般两种:
    1、使用 $front->setParam(‘useDefaultControllerAlways’, true); 这样每次找不到的方法都会访问默认的controller,这个默认的controller可以通过$front->setDefaultControllerName(); 方法来设置。
    2、使用ErrorController的plugin,具体的可以阅读这个地方的文档,在ZF的RC1版本后,这个plugin是默认打开的。
    文档地址:http://framework.zend.com/manual/en/zend.controller.plugins. html#zend.controller.plugins.standard.errorhandler (7.5.10.1文档)

    不过我遇到过一些问题,比如使用第一种方法,会导致各个controller里面调用$this->_forward()方法的时候,指定任何controller参数都不会生效。在使用第二种方法时,按照官方文档会报错,$this->_getRequest()方法不存在。

    基本知道的就是这些。

  14. Lewis says:

    请教一下博主Michael大哥,用ZF开发MIS系统,里面设置多个module,是不是在application目录下面设置多个目录,每个目录里面都有
    controllers/
    models/
    views/
    这样三个目录吗?

    能否给一个MIS系统的整体目录结构的例子

  15. Michael says:

    [Comment ID #17697 Will Be Quoted Here]

    目录结果其实ZF很灵活,通常是采用你说的这种方法。事实上,你要是反复看两遍ZF关于controller部分的文档,就会理解比较深入了:)

  16. Lewis says:

    谢谢指点!

  17. Michael says:

    [Comment ID #17703 Will Be Quoted Here]

    不客气,欢迎常来交流,共同学习和进步。

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image