比方說,我有一個「image.jpg」存儲在資產文件夾中,路徑是「www.example.com/public」,我只需要圖像的字符串路徑。我如何得到這個?我不希望html標籤只有路徑字符串。如何獲取Cakephp中圖片的完整URL?
回答
在嘗試使用不帶結果的各種CakePHP全局常量(ROOT,WEBROOT_DIR,WWW_ROOT,CSS等)後,通常的解決方案似乎可以在$ this-> webroot屬性中找到,該屬性返回應用程序webroot的路徑。因此,對於不同的情況下,上面我們可以在我們的佈局,觀點或元素:
一個簡單的圖片標籤:
<img src="<?php echo $this->webroot; ?>img/foo.gif" .../ >
表格內的一個背景圖像單元格的老路上:
<td background="<?php echo $this->webroot; ?>img/foo.gif">
An的類型= 「圖像」 輸入:
<input type="image" src="<?php echo $this->webroot; ?>img/go_btn.gif" ... />
爲我工作。需要注意的是,一些全局變量從2.4開始已被棄用:http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html –
注意:此解決方案不是插件安全的。 – MjrKusanagi
$this->Html->url('/img/image.jpg') // returns /cake/app/img/image.jpg
可選地將true
通過示例一用於完整基準URL。 (mysite.com/cake/app/img/image.jpg
)
WWW_ROOT . 'img/image.jpg' // full/path/to/webroot/img/image.jpg
嘿,我正在嘗試Html-> url,但我得到了「調用一個非對象的成員函數url()錯誤。我已經包含了「public $ helpers = array('Html','Form');」在我的控制器中... –
@JohnathanAu你使用的是什麼版本的CakePHP? – thaJeztah
最新的一個。 –
我覺得你需要自己寫作。如果您知道文件和目錄於它所駐留的名字,什麼是從做以下阻止你:
$imageUrl = sprintf("%s/%s/%s", FULL_BASE_URL, "public", $imageName);
// this produces http://www.example.com/public/image.png
檢查由CakePHP中提供了這個常數2.0+ FULL_BASE_URL
click here知道全部
查看HtmlHelper::image()
的源代碼,看看這個幫手是如何創建正確的URL的;稍加修改,這是它是如何實現的:
$imageUrl = $this->assetUrl('image.jpg', array(
// note: only required if you need the
// URL -including- http://example.com
'fullBase' => true,
'pathPrefix' => IMAGES_URL
));
的助手:: assetUrl(源代碼)可以在這裏找到:https://github.com/cakephp/cakephp/blob/2.3.2/lib/Cake/View/Helper.php#L305
您可以使用:
$this->webroot.'img/abc.jpg'
$這個 - > webroot會爲您提供您應用的當前路徑。
在CakePHP中,通常圖像將存儲在webroot/img文件夾中。
這個link會對您有所幫助。
HtmlHelper::image(string $path, array $options = array())
參數:
$path (string) – Path to the image.
$options (array) – An array of html attributes.
希望你覺得這是有用的。
我會用
$this->Html->url('/', true).'img/image.jpg';
$imageUrl = $this->Html->assetUrl('image.jpg', array( // note: only required if you need the // URL -including- http://example.com 'fullBase' => true, 'pathPrefix' => IMAGES_URL ));
這個工程,即使您正在使用的主題
在CakePHP 3版本,你可以使用url幫手:
$this->Url->build('/img/image.jpg');
在CakePHP 3(從版本3.2.4開始),您可以使用url/image helper,但路徑不是必需的:
個$this->Url->image('image.jpg');
或其他資產:
// Outputs /js/app.js
$this->Url->script('my.js');
// Outputs /css/app.css
$this->Url->css('my.css');
- 1. 如何獲取PHP中的完整URL?
- 2. 在網頁中獲取圖片的完整url
- 3. Facebook的圖形API圖片 - 獲取完整URL以.jpg
- 4. 如何從SPWEB獲取完整的URL
- 5. 如何從URI獲取完整的URL?
- 6. iphone sdk如何從照片庫中獲取完整的圖像?
- 7. 如何從python中的資源URL獲取完整的URL
- 8. 從回形針獲取完整圖片url
- 9. 如何在php中獲取完整的引用URL url
- 10. 如何從ImageView中獲取圖片URL
- 11. 如何從URL中獲取圖片
- 12. 如何獲取用作背景的圖像的完整URL?
- 13. 在Delphi中獲取完整的URL TWebmodule
- 14. 如何在Ajax.BeginForm完成後獲取完整的AJAX URL請求?
- 15. 如何在cakephp中獲取url參數?
- 16. 如何通過joomla中的ID獲取文章的完整url?
- 17. 如何從web api url獲取圖片?
- 18. 如何從url獲取圖片?
- 19. 我如何通過URL獲取圖片?
- 20. 如何獲取Wowza模塊中的完整流URL?
- 21. 如何在Rails 3.2中獲取資源的完整URL?
- 22. 如何在asp.net中獲取完整的請求URL
- 23. 如何在JSP中獲取完整的URL
- 24. 如何在jsp頁面中獲取完整的URL
- 25. 獲取的SoundCloud完整的URL
- 26. 如何獲取url http://graph.facebook.com/{ANYUSERID}/picture的提取圖片url?
- 27. 從相機或相冊中獲取完整大小的圖片
- 28. 獲取完整的URL與#tag
- 29. 獲取完整的URL地址
- 30. 獲取完整的請求URL - ASP.NET MVC
你想說的是,文件image.jpg文件存儲在你給的路徑? –
對不起,你的意思是? –
請詳細說明您的問題。或者說清楚 –