CakePHP 3.x 访问来自 URL 的图像
CakePHP 3.x Access Images from URL
CakePHP 版本:3.6
系统:Windows 10 WampServer 3.1.3 (Apache)
我无法从 url 访问图像,即如果我输入:
http://www.n.co.nz/img/someimage.png 我收到 404 未找到。我绝对能够在 CakePHP 2.0 中执行此操作,但无法在 CakePHP 3.6 中执行此操作。
图像"someimage.png"存储在project/webroot/img/someimage.png
我可以通过 url 直接访问 css 文件,即
http://n.co.nz/css/template_pages.css
使用 cakePHP 方式时图像加载正常,即
<?= $this->Html->image('someimage.png'); ?>
有什么想法吗?这是 .htaccess 还是路由问题?我的.htaccess如下
SourceFiles/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/ [L]
</IfModule>
Webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
更新
作为对 Peshraw 的回应,这是 config/app.php 文件中我的 'App' 数组的内容:
'App' => [
'namespace' => 'App',
'encoding' => env('APP_ENCODING', 'UTF-8'),
'defaultLocale' => env('APP_DEFAULT_LOCALE', 'en_US'),
'defaultTimezone' => env('APP_DEFAULT_TIMEZONE', 'UTC'),
'base' => false,
'dir' => 'src',
'webroot' => 'webroot',
'wwwRoot' => WWW_ROOT,
//'baseUrl' => env('SCRIPT_NAME'),
'fullBaseUrl' => false,
'imageBaseUrl' => 'img/',
'cssBaseUrl' => 'css/',
'jsBaseUrl' => 'js/',
'paths' => [
'plugins' => [ROOT . DS . 'plugins' . DS],
'templates' => [APP . 'Template' . DS],
'locales' => [APP . 'Locale' . DS],
],
];
回复顶戴夫
蛋糕插入图片时生成的HTML蛋糕方式是:
<img src="/img/nad_home_logo.png" id="nad_top_logo" alt="">
@Peshraw H. Ahmed
Are you sure you don't have typo in filename?
不...我为图像添加了 png 而不是 jpg。 CakePHP 在尝试使用 URL
直接加载图像时告诉我以下内容
Error: ImgController could not be found. Error: Create the class
ImgController below in file: src\Controller\ImgController.php
这让我很失望,我认为蛋糕没有将图像资源调用路由到 webroot/img 文件夹。我猜它正在扫描那个文件夹,没有找到资源然后传播以查看是否有一个知道该做什么的 imgController?
感谢您的帮助!
CakePHP 版本:3.6 系统:Windows 10 WampServer 3.1.3 (Apache)
我无法从 url 访问图像,即如果我输入: http://www.n.co.nz/img/someimage.png 我收到 404 未找到。我绝对能够在 CakePHP 2.0 中执行此操作,但无法在 CakePHP 3.6 中执行此操作。
图像"someimage.png"存储在project/webroot/img/someimage.png
我可以通过 url 直接访问 css 文件,即 http://n.co.nz/css/template_pages.css
使用 cakePHP 方式时图像加载正常,即
<?= $this->Html->image('someimage.png'); ?>
有什么想法吗?这是 .htaccess 还是路由问题?我的.htaccess如下
SourceFiles/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/ [L]
</IfModule>
Webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
更新
作为对 Peshraw 的回应,这是 config/app.php 文件中我的 'App' 数组的内容:
'App' => [
'namespace' => 'App',
'encoding' => env('APP_ENCODING', 'UTF-8'),
'defaultLocale' => env('APP_DEFAULT_LOCALE', 'en_US'),
'defaultTimezone' => env('APP_DEFAULT_TIMEZONE', 'UTC'),
'base' => false,
'dir' => 'src',
'webroot' => 'webroot',
'wwwRoot' => WWW_ROOT,
//'baseUrl' => env('SCRIPT_NAME'),
'fullBaseUrl' => false,
'imageBaseUrl' => 'img/',
'cssBaseUrl' => 'css/',
'jsBaseUrl' => 'js/',
'paths' => [
'plugins' => [ROOT . DS . 'plugins' . DS],
'templates' => [APP . 'Template' . DS],
'locales' => [APP . 'Locale' . DS],
],
];
回复顶戴夫
蛋糕插入图片时生成的HTML蛋糕方式是:
<img src="/img/nad_home_logo.png" id="nad_top_logo" alt="">
@Peshraw H. Ahmed
Are you sure you don't have typo in filename?
不...我为图像添加了 png 而不是 jpg。 CakePHP 在尝试使用 URL
直接加载图像时告诉我以下内容Error: ImgController could not be found. Error: Create the class ImgController below in file: src\Controller\ImgController.php
这让我很失望,我认为蛋糕没有将图像资源调用路由到 webroot/img 文件夹。我猜它正在扫描那个文件夹,没有找到资源然后传播以查看是否有一个知道该做什么的 imgController?
感谢您的帮助!