如何仅使用模型在 cakephp 中创建导航?

发布于 2024-12-11 09:46:13 字数 960 浏览 0 评论 0 原文

标题说明了一切。我将拥有如下所示的导航,它是从数据库动态创建的。

<ul>
    <li><a href='controller/view/1'>1</li>
    <li><a href='controller/view/2'>2</li>
    <li><a href='controller/view/3'>3</li>
</ul>

除活动页面外,每个页面上的内容都应该相同(逐页,我的意思是每个控制器上)。这是我第一次使用 cakephp。

这是我的元素文件:

<ul id="left-navigation">
<?php
App::import('Model','Godina');
$navigacija = $this->Godina->find('all');
foreach($navigacija as $nav){
echo '<li>' . $this->html->link($nav['Godina']      ['godina'],array('controller'=>'godina','action'=>'program',$nav['Godina']['godina'])
) . '</li>';
}

?>

这就是我收到的错误。

Fatal error: Call to a member function find() on a non-object in     /Applications/MAMP/htdocs/cakephp/app/View/Elements/godineNavigacija.ctp on line 4

由于我对 cakephp 核心的了解很低,所以它不起作用。 谢谢。

Title says all. I'll have navigation that looks like this and it's dynamically created from database.

<ul>
    <li><a href='controller/view/1'>1</li>
    <li><a href='controller/view/2'>2</li>
    <li><a href='controller/view/3'>3</li>
</ul>

It should be same on every page (by page, I mean on every controller), except for the active one. It's my first time in cakephp.

This is my elements file:

<ul id="left-navigation">
<?php
App::import('Model','Godina');
$navigacija = $this->Godina->find('all');
foreach($navigacija as $nav){
echo '<li>' . $this->html->link($nav['Godina']      ['godina'],array('controller'=>'godina','action'=>'program',$nav['Godina']['godina'])
) . '</li>';
}

?>

And this is the error that I am getting.

Fatal error: Call to a member function find() on a non-object in     /Applications/MAMP/htdocs/cakephp/app/View/Elements/godineNavigacija.ctp on line 4

And since my knowledge of cakephp core is quite low, it just doesn't work.
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望她远 2024-12-18 09:46:13

App::import 只是加载文件,就像 PHP 的 include 一样,它不会实例化对象。所以 $this->Godina 不存在。您可以这样做:

$Godina = ClassRegistry::init('Godina');
$Godina->find(...);

为了正确的 MVC 分离,您不应该在视图中使用模型。您可以在 AppController::beforeFilter 中获取并设置数据,因此它会在每个页面上执行此操作,然后在视图中输出。或者,查看 requestAction

App::import just loads the file, like PHP's include, it does not instantiate an object. So $this->Godina does not exist. You can do this:

$Godina = ClassRegistry::init('Godina');
$Godina->find(...);

For proper MVC separation, you should not use models in the view though. You can fetch and set the data in AppController::beforeFilter, so it does it on every page, then output in the view. Alternatively, look into requestAction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文