How to keep a controller item in static part of the page?

Asked

Viewed 122 times

1

It’s the following guys, I have a page of the project that serves as a standard for all the others, a Layout.. However I wish to add an item (informative) in it, and it only remains on the home page and in the CRUD of the informative, when it goes to other pages that occurs the controller exchange, gives error and the informative disappear. Could someone help??

layout page:

<div class="col-md-8">
    <div id="c-slide" class="carousel slide auto panel">

        <div class="carousel-inner">
            <?php foreach ($informativos as $index => $informativo){ ?>

                <div class="item text-center<?php echo ($index === 0 ? " active" : "") ?>" style="width: 90%;margin-left:5%">
                    <div class="alert alert-block alert-<?php echo $informativo['Informativo']['tipo']; ?> fade in">                                    
                        <?php echo $informativo['Informativo']['texto']; ?>
                    </div>
                </div>                        

            <?php } ?>
        </div>
        <a data-slide="prev" href="#c-slide" class="left carousel-control">
            <i class="fa fa-angle-left"></i>
        </a>
        <a id="nextInformativo" data-slide="next" href="#c-slide" class="right carousel-control">
            <i class="fa fa-angle-right"></i>
        </a>
    </div>
</div>

App Controller:

public $helpers = array(
        'Gravatar.Gravatar',
        'Html', 
        'Form', 
        'Session'
);

public $components = array(
        'Auth' => array(
                'authenticate' => array('Saml.Saml')
        ),
        'Session',
        'Cookie');

var $uses = array('User');

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

    $this->set('convertTime', $this->convertTime);

    if ($this->Saml){
        if ($this->Saml->isAuthenticated()) {               
            $infoWSO2 = $this->Saml->getAttributes();

            if($this->Session->read('UsuarioLogado') == NULL){
                 $userSistema = $this->User->atualizaSessao($infoWSO2);

                 $this->Session->write('UsuarioLogado', $userSistema);
            }

            $this->set('login_user', $infoWSO2);
        }
    }
}

1 answer

1


You should make your query in the bank in Appcontroller, more or less like this:

public function beforeRender() {

    // sua query
    $query = array(
        'order' => array(
            ...
        ),
        'conditions' => array(
            ...
        ),
        'recursive' => 1
    );

    $data = $this->SeuModel->find('all', $query);
    $this->set(compact('data'));
}

So it will be available in all views.

It is not a practice considered correct, because you will have this query in all your controllers and actions, and may cause a certain slowness in your application.

Most correct, would be you save this data to a Session, and query it in the view, so it would not impact the database.

  • In fact it is an internal intranet of the company, and this item "informative" is just a common text, which will not generate slowness.. I will test here and soon I give a return. Thanks

  • So, at first it’s correct, but in practice it’s making a mistake.. You’re saying that I’m calling the find() function on a non-eobject, but when I capture the variable created through var_dump, all the information is in the variable, it’s all right.. I don’t know what it could be..

  • Don’t forget to declare in your app controler public $uses = array('Seumodel'); Out of the beforeRender. Do this and tell us if it worked. I hope I helped :D

  • I’m already using the $uses variable: var $uses = array('User'), how do I do now? I’m sorry, but I’m kind of layabout it..

  • Post your code please, so it is easier to identify the problem. Edit your question, and put it there.

  • I edited my question

  • 1

    I managed to do based on your help, I only had to load the Model of the newsletters, thanks

  • 1

    Good! If you need to use more than one model in the same controller, simply add it to the array. var $uses = array('User','Outromodel','Terceiromodel');

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.