I have some questions on how I can improve this "action" (method) in "controller":
My template has a navbar with dynamic content (if the user is logged in a special button appears, his name appears in the navbar among other custumizações) also the integration of modes is dynamic based on whether the user is logged in or not, this insert in my controller calls some methods that will check if the user are logged and enter the data into the template.
I am inserting code that search the database (Query Builder) directly into the action.
I am using a comment line like "
//--------------------------
" to separate concepts and operations. Is it correct?I'm using one full template for every action (e.g.: One full template for action (method)
add
ofProducts
Controller) (Note: most of pages (add, edit, delete, index) use different .js and .css files).
Controller:
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
use Cake\ORM\TableRegistry;
class StoresController extends AppController
{
public function miniMap()
{
$setting = [
'fields' => ['id', 'banner_description', 'path_banner', 'url_redirect'],
'conditions' => ['banner_type_id' => 2],
'limit' => 1
];
$fullBanners = TableRegistry::get('Banners')
->find('all', $setting)->hydrate(false)->toArray();
$this->set('fullBanners', $fullBanners);
//-------------------------------------------------------------------------
$setting = [
'fields' => ['id', 'banner_description', 'path_banner', 'url_redirect'],
'conditions' => ['banner_type_id' => 1],
'limit' => 3
];
$smallBanners = TableRegistry::get('Banners')
->find('all', $setting)->hydrate(false)->toArray();
$this->set('smallBanners', $smallBanners);
//-------------------------------------------------------------------------
$this->set('userId', $this->Auth->user('id'));
//-------------------------------------------------------------------------
$this->set('username', $this->Auth->user('username'));
}
}
Template:
<?php
$this->layout = false;
?>
<!DOCTYPE html>
<html>
<head>
<?= $this->Html->charset() ?>
<?= $this->Html->meta('viewport','width=device-width, initial-scale=1.0') ?>
<?= $this->Html->meta('title',$pageTitle) ?>
<?= $this->Html->meta('favicon.ico','/cart.png', ['type' => 'icon']) ?>
<?= $this->Html->meta('keywords','') ?>
<?= $this->Html->meta('description','') ?>
<?= $this->Html->meta('robots','index,follow') ?>
<?= $this->Html->css('library/datepicker/css/datepicker.css') ?>
<?= $this->Html->css('library/bxslider-4-4.1.2/jquery.bxslider.css') ?>
<?= $this->Shrink->css(['styles/style.css', 'styles/menu-plugin.css']) ?>
<?= $this->Html->script('library/bxslider-4-4.1.2/jquery.bxslider.min.js',['defer' => true]) ?>
<?= $this->Html->script('actions/main.js',['defer' => true]) ?>
<?= $this->Shrink->fetch('css') ?>
</head>
<body>
<?= $this->element('Navbar/navbar_main') ?>
<div class="wrapper">
<div class="container">
<div class="row">
<?= $this->element('Body/categories') ?>
<?= $this->element('Body/stores') ?>
</div>
</div>
</div>
<?= $this->element('Footer/footer_information') ?>
<?php if ($userId == false): ?>
<?= $this->element('Modal/create_account_modal') ?>
<?= $this->element('Modal/login_modal') ?>
<?php else: ?>
<?= $this->element('Modal/logout_modal') ?>
<?php endif; ?>
</body>
</html>
block
option, there should always be a layout file - even if it's one-per-template IMO. \$\endgroup\$