douxieshang5577 2015-02-26 20:04
浏览 117
已采纳

PhalconPHP视图/布局/控制器

I am new in Phalcon .I create a PhalconPHP application which is get menu elements from database. I use a layout to create the menu which is called in index.volt, but the layout call directly the model function. I think this is not the best solution maybe i should use a controller between model and layout.

enter image description here

layout:

<?php

$menus = Menus::find();

foreach ($menus as $menu) {
    echo "<li>".$menu->name."</li>";
}

index:

<!DOCTYPE html>
<html>
    <head>
        <title>Phalcon PHP Framework</title>
    </head>

            <?php $this->partial("layouts/menus") ?>
        {{ content() }}

</html>

I would really appreciate, that somebody tell me what is the best solution for that.

  • 写回答

1条回答 默认 最新

  • doutuobao4004 2015-03-02 09:32
    关注

    In case of generating menu, you are looking probably for extending yout BaseController class. It's quite good practice for generating content you need on all your controllers like menu, meta-data or breadcrumbs.

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

    And set all your controllers to use that as default:

    class DefaultController extends BaseController { }
    

    Than in menus.phtml:

    <?php
    
        foreach ($menus as $menu) {
            echo "<li>".$menu->name."</li>";
        }
    

    should be enough. Looks better in Volt:

    <ul>
    {% for menu in menus %}
        <li>
            <a href="{{ menu.url }}">{{ menu.name }}</a>
        </li>
    {% enfor %}
    

    In case of more complex problems, like generating content only on 50% of your pages, you may want to put into View only parameters, eg.:

    $this->view->setVar('menus', array(
        'conditions' => 'controller = "' . $this->dispatcher->getControllerName() . '"'
    ));
    

    but that may be considered as not an elegant solution and is not preventing you from getting your hands on model in your View, what I assume you'd like to avoid. Slightly better would be setting an built query of queryBuilder and running its ->execute() in view loop, to not stress DB as long as it's not necessary.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮