0

I customised the base path for my site like this:

Router::scope('/', function (RouteBuilder $routes) {
    //$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);

    //My custom base path
    $routes->connect('/', ['controller' => 'Posts', 'action' => 'index', 'home']);

    //Connect the rest of PagesController URLs
    $routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);

    //I don't quite understand what this line does
    $routes->fallbacks(DashedRoute::class);
});

Then the URLs are correct when I redirect inside the controllers like this:

return $this->redirect(['controller' => 'Posts', 'action' => 'view', $data['post_id']]);

//output:
// localhost/jayblog/posts/view/2

But for external links and WWWROOT, it returns the wrong ones:

<li><a href="https://www.example.com" target="_blank" title="Twitter">LINK</a></li>

//output is wrong:
// localhost/jayblog/https://www.example.com

<?= $this->Html->image(WWW_ROOT.DS.'files'.DS.$photos[$i]->file_name); ?>

//output:
// jayblogD:\path\to\file.PNG

Anyone please suggest me how to fix the problem. Thank you!

Jay Nguyen
  • 342
  • 1
  • 2
  • 18

2 Answers2

0

Because WWW_ROOT is an absolute file system path and not a relative path like HTML::image expects. This will never work:

<?= $this->Html->image(WWW_ROOT.DS.'files'.DS.$photos[$i]->file_name); ?>

Debug your variables and see whats happening e.g.

var_dump(WWW_ROOT);

Please review Html::image() documentation https://book.cakephp.org/3.0/en/views/helpers/html.html#linking-to-images and pass it an array like the documentation requests or more likely in your case something like this:

<?= $this->Html->image('/photos-directory/'.$photos[$i]->file_name); ?>

Where photos-directory likely exists in something like /var/www/your-project/photos-directory

cnizzardini
  • 1,196
  • 1
  • 13
  • 29
  • oh I get what I did wrong with `image` helper now, thank you! Do you know what's the problem on external link? – Jay Nguyen Mar 15 '18 at 12:23
-1

try this one below replacing

$routes->fallbacks(DashedRoute::class); to $routes->fallbacks('InflectedRoute');

<?php
use Cake\Core\Plugin;
use Cake\Routing\Router;

Router::defaultRouteClass('Route');

Router::scope('/', function ($routes) {

    $routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
    $routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);


    $routes->fallbacks('InflectedRoute');
});

Router::extensions('json', 'xml');
/**
 * Load all plugin routes.  See the Plugin documentation on
 * how to customize the loading of plugin routes.
 */
Plugin::routes();
?>

It works in my end.

<ul>
<li><a href="http://www.cakephp.org/" target="_blank">
           <?php echo $this->Html->image('technology-use/cakephp.jpg', array('alt' => 'images'));?></a></li>
</ul>
distromob
  • 354
  • 1
  • 10
  • look like your base path was not modified so nothing was misbehaved. I tried routing extension but it does not help. – Jay Nguyen Mar 13 '18 at 03:58