<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
havokinspiration / cakephp-actions-class example snippets
// in src/Application.php
namespace App;
use Cake\Core\Plugin;
use Cake\Http\BaseApplication;
class Application extends BaseApplication
{
public function bootstrap()
{
parent::bootstrap();
Plugin::load('HavokInspiration/ActionsClass', ['bootstrap' => true]);
}
}
// in src/Controller/PostsController.php
namespace App\Controller;
use Cake\Controller;
class PostsController extends Controller
{
public function index() {}
public function add() {}
public function edit($id) {}
}
// in src/Controller/Posts/IndexAction.php
namespace App\Controller\Posts;
use HavokInspiration\ActionsClass\Controller\Action;
class IndexAction extends Action
{
public function execute() {}
}
// in src/Controller/Posts/AddAction.php
namespace App\Controller\Posts;
use HavokInspiration\ActionsClass\Controller\Action;
class AddAction extends Action
{
public function execute() {}
}
// in src/Controller/Posts/EditAction.php
namespace App\Controller\Posts;
use HavokInspiration\ActionsClass\Controller\Action;
class EditAction extends Action
{
public function execute($id) {}
}
Plugin::load('HavokInspiration/ActionsClass');
// in src/Controller/Posts/EditAction.php
namespace App\Controller\Posts;
use HavokInspiration\ActionsClass\Controller\Action;
class EditAction extends Action
{
public function initialize()
{
$this->loadComponent('Flash');
}
public function execute($id)
{
// some logic
$this->Flash->success('Post updated !');
}
}
// in src/Controller/Posts/EditAction.php
// Assuming that `Admin` is a routing prefix
namespace App\Controller\Admin\Posts;
use HavokInspiration\ActionsClass\Controller\Action;
class EditAction extends Action
{
public function execute($id)
{
}
}
// in tests/TestCase/Controller/Posts/IndexActionTest.php
namespace App\Test\TestCase\Controller;
use Cake\TestSuite\IntegrationTestCase;
class IndexActionTest extends IntegrationTestCase
{
public function testIndexAction()
{
$this->get('/posts');
$this->assertResponseOk();
}
}