如何处理 Symfony 2 中的相对路径? [英] How to deal with relative paths in Symfony 2?

查看:26
本文介绍了如何处理 Symfony 2 中的相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据您执行脚本的方式:控制台或网络,您似乎获得了不同的相对路径.

It seems like you get different relative paths depending on how you are executing the script: console or web.

我觉得这很烦人,你对此没有任何问题吗?

I find this extremely annoying, haven't you had any issues with this?

每次我需要复制/移动/删除文件时,我都必须考虑我是在 cli 还是 web 上,是否将其保存为 cli 或 web 路径等.

Every time I need to copy/move/delete files, I have to be thinking if I'm on cli or web, if I saved it as cli or web path, etc.

例如:假设有人上传了一张图片.您可能会使用这种路径存储图像:uploads/picture-123.jpeg.然后 cronjob 决定移动图片或其他东西.首先,它必须将其添加到源路径:web/.它还需要一个以 web/ 开头的目标路径.移动后,它会想将新路径保存在相应的数据库行或文档中,因此必须从目标路径中去除web/.

Eg: Say someone uploads an image. You will probably store the image with this kind of path: uploads/picture-123.jpeg. Then a cronjob decides to move the picture or something. First it will have to preppend this to the source path: web/. It will also need a target path, that begins with web/. After moving it, it will want to save the new path in the corresponding database row or document, so it will have to strip web/ from the target path.

有什么想法吗?

推荐答案

对于处理文件,定义路径的最佳做法是使用绝对路径,从 %kernel.root_dir% 开始:>

For handling files the best practice for paths defining is to use absolute ones, that start from %kernel.root_dir%:

parameters:
    your_upload_path: %kernel.root_dir%/../web/uploads/

这篇关于如何处理 Symfony 2 中的相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆