访问默认的CakePHP网站时,会转到"home.ctp"页面.
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Run Code Online (Sandbox Code Playgroud)
我想在那里添加一些元素(比如博客帖子),所以我想我可以将它添加到PagesController()类:
public function home() {
$this->set('blogposts', $this->Blogpost->find('all'));
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
那么:在主页(或任何其他页面)上添加这样的东西的正确方法是什么
Pie*_*rre 11
首选项是为主页创建自定义路由,但您也可以覆盖PagesController的显示功能
选项1 :(首选方法)
Router::connect('/', array('controller' => 'mycontroller', 'action' => 'myaction'));
Run Code Online (Sandbox Code Playgroud)
选项2
Router::connect('/', array('controller' => 'pages', 'action' => 'home'));
Run Code Online (Sandbox Code Playgroud)
选项3:
class PagesController {
function display()
{
// default controller code here
// your custom code here
}
}
Run Code Online (Sandbox Code Playgroud)
最后一个选项是在您的视图中使用requestAction,但不推荐使用它,因为它具有巨大的性能缺陷
选项4 :(不推荐)
$newsitems = $this->requestAction(array('controller' => 'newsitems', 'action' => 'getlatestnews', 'limit' => 10));
Run Code Online (Sandbox Code Playgroud)