Authentication – podstawowe komendy

W szablonie:

<?php
  $user_id = $this->getRequest()->getAttribute('identity')->getIdentifier();
?>

W kontrolerze:

$user_id = $this->Authentication->getIdentity()->getIdentifier();

$role = $this->Authentication->getIdentity()->get('role');

$role == 'admin' || 'user'

Wyłączenie z dostępu po zalogowaniu

use Cake\Event\EventInterface;


public function beforeFilter(EventInterface $event): void
{
        parent::beforeFilter($event);

        if(!$this->is_admin) {
            $this->Flash->error(__('Nie ma takiej strony'));
            $event->setResult($this->redirect('/'));
            return;
        }

        $this->Authentication->addUnauthenticatedActions(['edit', 'changePassword' ]);
    }

Aktualizacja URL

let url = new URL(location.href);

$("#proj-tabs").find(".nav-link").on('click', function() {

    var tab_name = $(this).attr('data-bs-target').replace("#", "");

    url.searchParams.set("s_tab", tab_name);

    history.replaceState(null, null, url.href);

});

NipTrait

validateNip($nip)   - Sprawdza poprawność     - Invoice::validateNip('1234567890') → true 
normalizeNip($nip)  - Usuwa myślniki, spacje  - Invoice::normalizeNip('123-456-78-90') → '1234567890'
formatNip($nip)     - Formatuje XXX-XXX-XX-XX - Invoice::formatNip('1234567890') → '123-456-78-90'
generateRandomNip() - Generuje losowy - NIP   - Invoice::generateRandomNip() → '857-234-19-23'
getTestNip()        - Stały NIP testowy       - Invoice::getTestNip() → '123-456-78-16'
isForeignNip($nip)  - Czy NIP zagraniczny     - Invoice::isForeignNip('9912345678') → true 
getNipInfo($nip)    - Debug info Zwraca tablicę z detailami 

seller_nip_formatted - Virtual field - $invoice->seller_nip_formatted → '123-456-78-90' 
buyer_nip_formatted  - Virtual field - $invoice->buyer_nip_formatted → '123-456-78-90'

Czytaj dalej NipTrait

Dostęp do innej tabeli z Modelu

W modelu Table nie można użyć metody $this->fetchTable(„Projects”)  – ta metoda dostępna jest tylko dla Controllers.

src/Model/Table/InvoicesTable.php

use Cake\ORM\TableRegistry;

class InvoicesTable extends Table
{

  public function getProjects() { 
    
    $projects = TableRegistry::getTableLocator()->get('Projects')
                  ->find()
                  ->where(['is_active' => 1])
                  ->all();
    ....
  } // getProjects()
...

} // class

CakePHP 5 – Formularz – Powiązania modeli

$this->Form->create($article);

// Article controls.
echo $this->Form->control('title');

// Author controls (belongsTo)
echo $this->Form->control('author.id');
echo $this->Form->control('author.first_name');
echo $this->Form->control('author.last_name');

// Author profile (belongsTo + hasOne)
echo $this->Form->control('author.profile.id');
echo $this->Form->control('author.profile.username');

// Tags controls (belongsToMany)
// as separate inputs
echo $this->Form->control('tags.0.id');
echo $this->Form->control('tags.0.name');
echo $this->Form->control('tags.1.id');
echo $this->Form->control('tags.1.name');

// Inputs for the joint table (articles_tags)
echo $this->Form->control('tags.0._joinData.starred');
echo $this->Form->control('tags.1._joinData.starred');

// Comments controls (hasMany)
echo $this->Form->control('comments.0.id');
echo $this->Form->control('comments.0.comment');
echo $this->Form->control('comments.1.id');
echo $this->Form->control('comments.1.comment');

$this->Form->end();

Czytaj dalej CakePHP 5 – Formularz – Powiązania modeli