dongxuxian6930 2012-08-15 16:43
浏览 93
已采纳

php声明应用程序根目录

I have a little problem: I began a project as a subdirectory in a larger web project. Thus the web file path is something like /../myProject. But things have progressed and I've realized that this should be its own project. However, I'd like to be able to keep it where it (as a sub-directory) also make it a sub-domain wherein myProject becomes the root. (There is also the possibility that my project will be mirrored at a library site, where it will once be in a sub-directory).

The problem I having with all this is that in some cases I have html_partial files, (for instance for the header or footer). But the relative path of these partials differs depending on where you are in the file tree. I originally solved this by always going back to the root.

But now, you see, depending on where my project lives, the root will be different. What I'd like to do is declare myProject as the "application root" and then be able to use relative paths based on this application root rather the than the web root'. This way, all of the relative paths within 'myProject' will work no matter wheremyProject` lives in the web path.

Does PHP have a way to declare something like an Application Root if so, can you explain it me or direct me to its documentation. Thanks!

  • 写回答

2条回答 默认 最新

  • duanlongling5308 2012-08-15 16:59
    关注

    I usually do something lile this in the front controller:

    define('APPLICATION_PATH', realpath(__DIR__));
    

    Then you can do things like:

    set_include_path(APPLICATION_PATH . '/include');
    

    Or:

    $fp = fopen(APPLICATION_PATH . '/path/to/some/file', 'r');
    

    If your app doesn't make use of a front controller, you could define an environment variable in your vhost config or .htaccess:

    SetEnv APPLICATION_PATH /full/path/to/my/app
    

    And then use:

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

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图