dongxun3424 2014-02-19 20:00
浏览 10
已采纳

从模型应用程序范围向cakephp布局添加值

thanks for reading, I know the question might sound fairly common, and well I wont deny the fact that maybe I'm just formulating what i want wrongly.

Lets start by shooting it straight, then specifying.

I have a cakephp app with 2 layouts, one layout renders the whole "public" page, and lets call it admin "admin" layout that will render only actions to authenticated users.

in my admin layout, I am printing an element which is the navigation bar.

what I want to do is, without setting on every single controller, the options to set a variable containing the navigation bar values (yes dynamically filled from a specific model)

I want to be able, to set a variable, which will contain a list of values gotten from a model.

The Model is called "Section", which is a table, that contains a list of "sections" of the application.

this navigation bar, needs to print the values of each section, so I need them to be dynamic, hence, I think (again I might be wrong) i need to set that variable somewhere, to make it available to the element, so when the layout "admin" is rendered, the menu bar, is actually filled with the available values of the sections.

I tried doing a Configure::write on AppController, but no dice, it just allowes me to use a variable on controllers, when what I want to do is, loop through the arrah "sections_for_menu" or whatever we call it, and then print the options avaliable on the menu bar.

so you are a bit more comfortable with the idea, the nav bar is a bootstrap based "navbar" with "dropdowns".

i should be able to

<ul class="mycoolclass">
<?php    
foreach($sections as $section) { 
    echo '<li><a href="whatever">" . $section . "</a></li>'; 
}
</ul>

and thus, printing each value on a new list with its link, and whatnot.

I have been reading with no luck, and have to admin that I am myself fairly new to cakephp, been using it for no longer than 2 weeks.

Any help reaching a solution to this need, is highly appreciated.

UPDATE:

Hi @nunser, thank you very much for your reply.

indeed I'm using an element this is my layout "base"

<body>
<?php echo $this->element('admin_navbar'); ?>
<!-- container -->
<div class="container-fluid">
<!-- , array('element' => 'flash') -->
<?php echo $this->Session->flash('flash'); ?>
<?php /*echo $this->fetch('content');*/echo $content_for_layout; ?>
</div>
</body>

I'll try your suggestion and see how it goes

I have a controller "SectionsController" which is in charge of well all Section related actions on the app, what I need is to set in the global variable, a list of sections, so I can print the link inside my navbar!

lets assume the following scenario

$this->set('sections_for_navigation', $this->Section->find('list', array('fields' => array('id', 'name'))));

so then i can access the variable $sections_for_navigation from my element, and render the list of sections.

[19-02-2014 - update] tried it.

based on what @nunser suggested, im actually able to, beforeFilter, setting the value, as if i var_dump it, it actually gives me the array as i expected.

public function beforeFilter(){
    $this->loadModel('Section');
    $secciones = $this->Section->find(
        'list', array(
            'fields' => array(
                'id',
                'name'
            )
        )
    );
    $this->set('navigation', $secciones);

Then call

public function beforeFilter() {
    parent::beforeFilter();
}

from the controllers, did the trick!, now, do i have to add a beforeFilter to every controller to share the navigation through all the app?, is there a way to avoid having to add that method to every single controller? (not that it bothers me, so far it does what i need,which is actually great)

  • 写回答

1条回答 默认 最新

  • douzheng1853 2014-02-19 20:20
    关注

    For those kind of navigation things, I do it in beforeFilter or beforeRender in the AppController.

    public function beforeFilter() {
        //queries and stuff that gets the array for navigation
        $this->set('mainNavigationBar', $navigation)
        //remember to pass that variable to the element in the view
    }
    

    As of to where, beforeFilter or beforeRender, it depends on what type of rule you want to stablish. For example, if you will have same elements for navigation everywhere, be it that the user has permission or not, or if you might want to alter the navigation variable depending on the action being executed, I'd go with doing it in beforeFilter. That way you can tweak things in navigation if, for example, the user doesn't have permission to access any Section (totally making up the model relation here). Though for that, you might want to keep access to the navigation array in the controller.

    Tweaked example

    protected $_mainNav = array();
    public function beforeFilter() {
        //queries and stuff that gets the array for navigation
        $this->set('mainNavigationBar', $navigation)
        //remember to pass that variable to the element in the view
        $_mainNav = $navigation;
    }
    
    
    //other controller
    public function randomAction() {
        //some reason that makes you modify the navigation
        unset($_mainNav[0]); //making that up...
        $this->set('mainNavigationBar', $navigation)
    }
    

    Now, if you won't change the navigation values (once you've added them dynamically), then go with beforeRender, that way you can check permissions and other stuff before bothering with queries for navigation (example to follow would be the first one).

    If values of the navigation change per controller, overwrite the function like

    RandomController

    public function beforeFilter() {
        parent::beforeFilter();
        $_mainNav = array('no nav'); //example
    }
    

    And that's it. Don't know if you need more detail than that, if so, please explain you problem a little more. Oh, and you mention "to make it available to the element", so I'm guessing you're using an element for the navigation bar. If not, please do.

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

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答