PHP code example of havokinspiration / cakephp-actions-class

1. Go to this page and download the library: Download havokinspiration/cakephp-actions-class library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?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();
    }
}

Configure::write('ActionsClass.strictMode', true);

src/
  Controller/
    Posts/
      AddAction.php
      EditAction.php
      IndexAction.php

tests
  /TestCase
    /Controller
      /Posts
        /IndexActionTest.php