- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是 Phalcon 的新手。我创建了一个 PhalconPHP 应用程序,它从数据库中获取菜单元素。我使用布局创建在 index.volt 中调用的菜单,但布局直接调用模型函数。我认为这不是最好的解决方案,也许我应该在模型和布局之间使用 Controller 。
布局:
<?php
$menus = Menus::find();
foreach ($menus as $menu) {
echo "<li>".$menu->name."</li>";
}
索引:
<!DOCTYPE html>
<html>
<head>
<title>Phalcon PHP Framework</title>
</head>
<?php $this->partial("layouts/menus") ?>
{{ content() }}
</html>
如果有人能告诉我最好的解决方案是什么,我将不胜感激。
最佳答案
在生成菜单的情况下,您可能正在寻找扩展您的 BaseController 类。在所有 Controller 上生成您需要的内容(例如菜单、元数据或面包屑)是一种很好的做法。
class BaseController extends \Phalcon\Mvc\Controller {
function initialize() {
$menus = Menus::find(array(
// you may want to condition query based on user cookie
// or controller you are in
'conditions' => 'controller = "' . $this->dispatcher->getControllerName() . '"'
));
// and set it as View variable to use it if you want
$this->view->setVar('menus', $menus);
}
}
并将所有 Controller 设置为默认使用它:
class DefaultController extends BaseController { }
比起在 menus.phtml 中:
<?php
foreach ($menus as $menu) {
echo "<li>".$menu->name."</li>";
}
应该够了。在 Volt 中看起来更好:
<ul>
{% for menu in menus %}
<li>
<a href="{{ menu.url }}">{{ menu.name }}</a>
</li>
{% enfor %}
如果出现更复杂的问题,例如只在 50% 的页面上生成内容,您可能需要将参数放入“仅查看”中,例如:
$this->view->setVar('menus', array(
'conditions' => 'controller = "' . $this->dispatcher->getControllerName() . '"'
));
但这可能被认为不是一个优雅的解决方案,并且不会阻止您在 View 中使用模型,我认为您希望避免这种情况。稍微好一点的是设置一个内置查询 queryBuilder并在 View 循环中运行它的 ->execute()
,只要没有必要,就不会给 DB 带来压力。
关于view - PhalconPHP View /布局/ Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28751741/
我是 phalconphp 的新手并遵循他们的教程,据我所知,我不需要创建特定的路由组件,并且它应该选择一个路由(如果存在)。我在这里显然可能犯了很大的错误,这意味着纠正我应该很容易!但到目前为止,唯
如何使用PhalconPHP 来向用户显示500错误? 我使用此代码显示404错误(显示错误404.volt View )和500错误(显示错误500.volt View )。 404错误页面将按预期
我刚刚开始开发一个简单的 Restful 服务。我的文件夹结构如下: root - /api --/api/customers.php 例如,在浏览器中我打算调用http://domain/api/c
SELECT * FROM (SELECT post.* FROM post ORDER BY post.timestamp DESC) as post GROUP BY post.topic_id
我像这样实例化我的数据库适配器: return new \Phalcon\Db\Adapter\Pdo\MySql(array( "host" => $options["host"],
我有一个正在记录应用程序的 webapp,我需要那里的备份/恢复/导入/导出功能。我用 laravel 成功地做到了这一点,但与 Phalcon 有一些并发症。我没有在 phalcon 中看到 nat
使用 PhalconPHP,是否可以使用带有注释元数据的 ORM 模型在数据库中创建表?例如,使用 symphony one 可以实现我想要的效果:http://symfony.com/doc/cur
我使用 PhalconPHP 开发了一个网站。该网站在我的本地计算机上运行良好,具有以下规范: PHP Version 7.0.22 Apache/2.4.18 PhalconPHP 3.3.1 还有
我正在使用很棒的 PhalconPHP Framework .我有不同的用户级别并加载正确的 Controller ,我在调度程序类中使用命名空间。 $di->set('dispatcher', fu
我刚刚开始研究一个简单的 restful 服务。我的文件夹结构是这样的: root - /api --/api/customers.php 例如在浏览器中我打算调用http://domain/api/
我有一个 games 表和两个表来存储他们的分数,game_scores 和 thirdparty_game_scores 具有完全相同的结构,如下所示: ----------------------
我有两个表: AuctionPackage $this->belongsTo('id', 'AuctionPackageTranslation', 'auction_package_id', NULL
好的,我有两张 table 。一个称为 Properties,另一个称为 PrInfo。 Properties 是基表,PrInfo 是另一个包含额外信息的表。 我想将它们连接在一起,以便我可以在网站
在 status 为 abanadoned 或 completed 的计划中查找节省的金额的正确语法是什么: $total = self::sum([ 'column' => 'amount_
我有两张 table ;加入的 Pages 和 PagesTranslation。我可以得到所有的数据和翻译;例如,在单个页面上,我可以通过 { page.pagestranslation.getTi
我正在使用 PhalconPHP,到目前为止我遇到的问题很少。 框架很好。 但是,我遇到了一个无法解决的问题。这可能是一件非常简单的事情,但我一直在兜圈子,找不到任何关于这个问题的引用。 问题是,如果
我正在使用 PhalconPHP 1.3.4 构建一个网络应用程序,我试图同时从多个表中选择数据,因为某些值通过关系存储在另一个表中。 我的查询在 MySQL 工作台中运行良好,但是当我尝试使用 Ph
如何将变量传递给主视图,而不仅仅是 Controller View 。 事实上,我想在所有 View 中共享一条数据。我怎样才能在 phalcon 中做到这一点。 例如,在 Laravel 中,我可以
我目前正计划为我的应用程序开发一个 REST API,我一直在研究现有的 PHP 框架,以及加快 PHP 执行速度的现有解决方案。 我的目标是在不影响开发速度的情况下获得最佳性能。 我一直在两者之间犹
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 5 年前。
我是一名优秀的程序员,十分优秀!