如何在laravel中定义路径变量?例如,我有一个webserver路径
https://s3-ap-southeast-1.amazonaws.com/profile_image
在我的HTML中我会:
<img style="width: 100%;padding:1px;border: 2px solid #555" src="https://s3-ap-southeast-1.amazonaws.com/myapps/profile_image/{{ $data->ap_thread_created_by}}/{{ $data->image_path }}">
但我希望在我的配置或其他地方获取路径..而不是硬编码路径。
答案 0 :(得分:1)
您可以在.env文件中定义环境变量:
IMAGES_PATH=https://s3-ap-southeast-1.amazonaws.com/myapps/profile_image
以任何方式获取它:
<img style="width: 100%;padding:1px;border: 2px solid #555" src="{{env('IMAGES_PATH')}}/{{ $data->ap_thread_created_by}}/{{ $data->image_path }}">
答案 1 :(得分:0)
假设$data
代表您的模型,例如Photo
,您可以向其添加accessor:
public function getStoragePathAttribute($value)
{
return 'https://s3-ap-southeast-1.amazonaws.com/myapps/profile_image/';
}
现在在您的刀片中,您可以使用:
<img style="width: 100%;padding:1px;border: 2px solid #555" src="{{ $data->storage_path }}{{ $data->ap_thread_created_by}}/{{ $data->image_path }}">
答案 2 :(得分:0)
function avatar_url($data) {
return "https://s3-ap-southeast-1.amazonaws.com/myapps/profile_image/{$data->ap_thread_created_by}/{$data->image_path}";
}
// src="{{avatar_url($data)}}"