我为我的网站定制了这样的基本路径:
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
任何人都建议我如何解决问题。谢谢!
答案 0 :(得分: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
之类的内容中答案 1 :(得分:-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>