infotechnohelp / cakephp-core_advanced
Plugin for CakePHP 3
Requires
- cakephp/cakephp: 3.7.*
- infotechnohelp/cakephp-authentication_advanced: dev-master#ccc8de4b474e945d218a5d6db4397ea2ba3ca4d1
- infotechnohelp/cakephp-json-api_advanced: dev-master#9c9c6fc196f82b9d43199f4beb4d5c520efbce85
- vlucas/phpdotenv: ^2.4
Requires (Dev)
- doctrine/instantiator: 1.0.*
- phpunit/phpunit: ^5.7|^6.0
- squizlabs/php_codesniffer: ^3.2
This package is auto-updated.
Last update: 2024-10-14 21:56:52 UTC
README
Structure
src/Controller/AppController extending structure
Core ← DefaultSkeleton ← Skeleton ← App ← Core\Api ← DefaultSkeleton\Api ← Skeleton\Api ← App\Api
src/Template/Layout/default.ctp extending structure
Core ← DefaultSkeleton ← Skeleton ← App
Implementation
1. Add plugin
src/Application.php → bootstrap()
$this->addPlugin('Infotechnohelp/Core', ['bootstrap' => true, routes' => true]);
Routes
config/routes.php
Router::defaultRouteClass(DashedRoute::class);
Configure::write('Infotechnohelp.Routes.Connected', ['app' => function (RouteBuilder $routes) {
$routes->connect('/', ['controller' => 'Home', 'action' => 'index', 'home']);
...
}]);
CSRF
Implement HTML form
src/Template/*/action.ctp
CakePHP FormHelper
echo $this->Form->create().
$this->Form->control('name').
$this->Form->submit('Submit', ['class' => 'class1 class2']).
$this->Form->end();
Plain HTML form
src/Controller/*Controller.php
public function action(){
$this->set('csrfToken', $this->getRequest()->getParam('_csrfToken'));
}
src/Template/*/action.ctp
<form action="" method="post">
<input type="hidden" name="_csrfToken" autocomplete="off" value="<?= $csrfToken ?>"/>
<input name="name">
<button type="submit">Submit</button>
</form>
Implement in AJAX call
src/Template/Layout/default.ctp
or any other .ctp
jQuery
POST data param _csrfToken
var csrfToken = <?= $this->request->getParam('_csrfToken'); ?>;
$.ajax({
method: 'POST',
data: {
_csrfToken: csrfToken
},
...
});
Header
var csrfToken = <?= $this->request->getParam('_csrfToken'); ?>;
$.ajax({
beforeSend: function (xhr) {
xhr.setRequestHeader('X-CSRF-Token', csrfToken);
},
...
});
JsonApi implementation
Add config
Api/...Controller.php
Request handler component should be loaded
$this->loadComponent('RequestHandler');
config/infotechnohelp.json-api.yml
pathPrefixes:
- api
- auth-api (automatically later)
Set ErrorHandlerMiddleware (JSON response structure)
Replace manually in src/Application.php
use Cake\Error\Middleware\ErrorHandlerMiddleware;
→ use Infotechnohelp\JsonApi\Middleware\ErrorHandlerMiddleware;