CakePHP 3.x Access Images from URL

CakePHP Version: 3.6 System: Windows 10 WampServer 3.1.3 (Apache)

I'm having trouble accessing images from url i.e. if I type: I get a 404 not found. I was definitely able to do this in CakePHP 2.0 can't get it working in CakePHP 3.6.

The image "someimage.png" is stored in project/webroot/img/someimage.png

I can access css files directly via url i.e.

Images are loading fine when using the cakePHP way i.e.

<?= $this->Html->image('someimage.png'); ?>

Any ideas? Is this a .htaccess or routing issue? My .htaccess are as follows


<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^$    webroot/    [L]
   RewriteRule    (.*) webroot/$1    [L]


<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]


In response to Peshraw, here's the contents of my 'App' array in the config/app.php file:

'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],

In response top Dave

The HTML that cake is generating when inserting an image the cake way is:

<img src="/img/nad_home_logo.png" id="nad_top_logo" alt="">

1 answer

  • answered 2018-07-11 21:21 codine

    @Peshraw H. Ahmed

    Are you sure you don't have typo in filename?

    Nope... I added png instead of jpg for the image. CakePHP was telling me the below when trying to load the image directly using the URL

    Error: ImgController could not be found. Error: Create the class ImgController below in file: src\Controller\ImgController.php

    This threw me off, I thought cake wasn't routing the image resource calls to the webroot/img folder. I guess it was scanning that folder, not finding the resource and then propagating to see if there's an imgController that knows what to do?

    Thanks for the help!