나는 AdminLTE 테마를 사용하고 있습니다. 헤더에 데이터베이스의 데이터가 필요한 기능을 추가하고 싶습니다. 케익 PHP 3에서 어떻게 할 수 있습니까? 쿼리를 호출하고 뷰에서 데이터를 가져 와서 수행했지만 mvc 위반입니다. 데이터베이스에서 데이터를 가져와 모든 뷰에서이 데이터를 사용하려면 어떻게해야합니까?
나는 당신이 케이크의 컨트롤러 이벤트 사용할 수 있다고 생각합니다 beforeRender()
내부 AppController
. 이 기능을 사용하면 확장 된 컨트롤러의 모든 방법에 대한 데이터를 간단히 출력 할 수 있습니다 AppController
. 이런 종류의 기능에 자주 사용합니다.
<?php
// src\Controller\AppController.php
namespace App\Controller;
use Cake\Controller\Controller;
use Cake\Event\Event;
class AppController extends Controller
{
/**
* Before render callback.
*
* @param \Cake\Event\Event $event The beforeRender event.
* @return \Cake\Network\Response|null|void
*/
public function beforeRender(Event $event) {
$this->loadModel('Notifications');
$notifications = $this->Notifications->find('all' , [
'conditions' => [
// get notifications for current user or default demo notification
'user_id' => $this->Auth ? $this->Auth->user('id') : 0
]
])
->toArray();
$this->set(compact('notifications'));
}
}
그런 다음 템플릿에서 $notifications
변수에 액세스 할 수 있습니다.
<div class="list-group">
<?php if(count($notifications)):foreach($notifications as $item): ?>
<div class="list-group-item">
<?= h($item->content) ?>
</div>
<?php endforeach;endif; ?>
</div>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다