0

我为我的网站自定义了基本路径,如下所示:

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);
});

然后,当我像这样在控制器内部重定向时,URL 是正确的:

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

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

但是对于外部链接 和WWWROOT,它会返回错误的链接:

<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

任何人都请建议我如何解决这个问题。谢谢!

4

2 回答 2

0

因为 WWW_ROOT 是绝对文件系统路径,而不是 HTML::image 所期望的相对路径。这永远不会奏效:

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

调试你的变量,看看发生了什么,例如

var_dump(WWW_ROOT);

请查看 Html::image() 文档https://book.cakephp.org/3.0/en/views/helpers/html.html#linking-to-images并将其传递给文档请求中的数组或更可能在您的情况是这样的:

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

照片目录可能存在于 /var/www/your-project/photos-directory 之类的位置

于 2018-03-14T18:41:17.220 回答
-1

试试下面这个替换

$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();
?>

它对我有用。

<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>
于 2018-03-13T03:13:45.100 回答