duanpie2414 2012-09-21 13:18
浏览 47
已采纳

在Zend Framework视图脚本中使用baseUrl()很重要吗?

Some people have reported issues with accessing, setting, or getting the right value from baseUrl() in a view script. But I'm wondering why it is necessary to use it at all, at least in a situation like mine where the ZF application is on a virtual private host (Amazon EC2) where I have full control of the directory structure and apache rewrite rules, as well as routes.

I know, for example, that in the filesystem foo.jpg lives in public/images/foo.jpg, and that the application's mod_rewrite will direct all requests to public - so in my view scripts it's a lot simpler/clearer and more efficient to write something like

<img src="/images/foo.jpg" />

instead of

<img src="<?php echo $this->baseUrl();?>/images/foo.jpg" />

What sort of future-proofing robustness or other benefit does the use of baseUrl() really provide? So far I haven't used it at all, and had no problem. But I've inherited some code that uses it, and my inclination is to strip out those uses whenever I'm editing a view script that contains them. Would I regret that later?

  • 写回答

2条回答 默认 最新

  • doufan9805 2012-09-21 14:17
    关注

    Used this way, it's not really useful, but on the other hand, using it this way

    echo $this->baseUrl('/images/foo.jpg')
    

    might prove to be useful in the future since you can add logic before printing the URL. Imagine that in a few years your website grows way more than you expected and you have to move all your static content to a Content delivery network (CDN) you will have to manually (or with search and replace) correct all your images/css/js instances URLs. With the baseUrl() (or as name it assetUrl()) you would just have to add your CDN's url and it will be fixed everywhere in your application.

    EDIT

    I found a use for the baseUrl() in the code you inherited :

    It would allow you to add a common URL part to all of your links and references, in the case that your site is not at the root of the domain

    i.e. : www.mysite.com/zf-app/
    

    In your config file you would just have to add

    resources.frontController.baseUrl = "/zf-app/"
    

    for it to work, and all of your links would be prepended with that part

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算