PHPackages                             havokinspiration/cakephp-actions-class - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. havokinspiration/cakephp-actions-class

ActiveCakephp-plugin[Framework](/categories/framework)

havokinspiration/cakephp-actions-class
======================================

Manage your Controllers actions as single classes in CakePHP

1.2.0(8y ago)121671MITPHPPHP &gt;=7.0.0CI failing

Since Jul 20Pushed 8y ago1 watchersCompare

[ Source](https://github.com/HavokInspiration/cakephp-actions-class)[ Packagist](https://packagist.org/packages/havokinspiration/cakephp-actions-class)[ RSS](/packages/havokinspiration-cakephp-actions-class/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (4)Dependencies (3)Versions (6)Used By (0)

CakePHP ActionsClass plugin
===========================

[](#cakephp-actionsclass-plugin)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.txt)[![Build Status](https://camo.githubusercontent.com/ef74381435b2247ca00ce615b104e07687e2a80f41eada9c0b1dacdc3053aa37/68747470733a2f2f7472617669732d63692e6f72672f4861766f6b496e737069726174696f6e2f63616b657068702d616374696f6e732d636c6173732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/HavokInspiration/cakephp-actions-class)[![codecov.io](https://camo.githubusercontent.com/bdc8e5e77a070d317a708b3862ddeebcac269ea229d0ee526fa9d2cc34755033/68747470733a2f2f636f6465636f762e696f2f6769746875622f4861766f6b496e737069726174696f6e2f63616b657068702d616374696f6e732d636c6173732f636f7665726167652e7376673f6272616e63683d6d6173746572)](https://codecov.io/github/HavokInspiration/cakephp-actions-class?branch=master)

This plugin gives you the ability to manage your CakePHP Controller actions as single classes. Each action of your Controllers will be managed in its own object.

Requirements
------------

[](#requirements)

- PHP &gt;= 7.0.0
- CakePHP 3.4.X

Installation
------------

[](#installation)

You can install this plugin into your CakePHP application using [Composer](https://getcomposer.org).

The recommended way to install it is:

```
composer require havokinspiration/cakephp-actions-class

```

Loading the plugin
------------------

[](#loading-the-plugin)

It is recommanded to load this plugin using the `bootstrap()` method of your application's `Application` class. This is needed if you want to be able to use [Integration testing](https://book.cakephp.org/3.0/en/development/testing.html#controller-integration-testing):

```
// 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]);
    }
}
```

**Loading the plugin bootstrap file is mandatory.**

Usage
-----

[](#usage)

By default, CakePHP controller management is based around one controller (which represents one part of your application, for instance "Posts") which is a single class containing one public method per actions needed to be exposed in your application:

```
// 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) {}
}
```

As your application grows, your controllers grow as well. In the end, on large applications, you can end up with big controller files with lots of content, making it hard to read through. You might even be in the case where you need to have methods specific to some actions in the middle of other methods dedicated to other actions.

This is where the **cakephp-actions-class** plugin is useful. When enabled, **you can have your controllers actions as single classes**.

So the `PostsController` example given above would become:

```
// 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) {}
}
```

Living in the following directory structure :

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

```

Your `Action` classes are only expected to hold an `execute()` method. It can receive passed parameters as in regular controller actions (meaning the URL `/posts/edit/5` will pass `5` to the argument `$id` in the `execute()` method of the `EditAction` class in our previous example).

#### Using the `bake` command-line to create Action classes

[](#using-the-bake-command-line-to-create-action-classes)

You first need to load the plugin in your **config/bootstrap\_cli.php** :

```
Plugin::load('HavokInspiration/ActionsClass');
```

You can then create an Action class file with the following command :

```
bin/cake bake action Posts/Index

```

The command expects the name to get the controller name and the action name separated by a forward slash. For instance, the above example would create a `IndexAction` file for the `Posts` controller.

You can also specify the routing prefix your controller action lives under by using the `--prefix` option :

```
bin/cake bake action Posts/Index --prefix Admin

```

If you want to create an action file for a plugin, you can use the `--plugin` option :

```
bin/cake bake action Posts/Index --plugin MyPlugin

```

You can of course use both together :

```
bin/cake bake action Posts/Index --plugin MyPlugin --prefix Admin

```

By default, baking an action class will generate the corresponding test file. You can skip the test file generation by using the `--no-test` boolean flag :

```
bin/cake bake action Posts/Index --no-test

```

Compatibility
-------------

[](#compatibility)

This plugin was designed to have a maximum compatibility with the regular CakePHP behavior.

### Fallback to CakePHP regular behavior

[](#fallback-to-cakephp-regular-behavior)

If you wish to use this plugin in an existing application, it will first try to provide a response using an `Action` class. If an action class matching the routing parameters can not be found, it will let CakePHP fallback to its regular behavior (meaning looking for a `Controller` class).

This also means that you can develop a plugin with controllers implementing this behavior without breaking the base application (since, when the **cakephp-actions-class** plugin is loaded, the Dispatcher will first try to load an `Action` class and fallback to the regular `Controller` dispatching behavior if it can not find a proper `Action` class to load).

### Everything you do in Controller can be done in an Action class

[](#everything-you-do-in-controller-can-be-done-in-an-action-class)

Under the hood, `Action` classes are instances of `\Cake\Controller\Controller`, meaning that **everything you do in a regular `Controller` class can be done in an `Action` class**.
Every events (like `beforeFilter` or `beforeRender`) a controller fires are also fired by `Action` classes.

#### Loading Components

[](#loading-components)

```
// 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 !');
    }
}
```

#### Actions in Controllers under a routing prefix

[](#actions-in-controllers-under-a-routing-prefix)

`Action` classes can live under a routing prefix or a plugin :

```
// 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)
    {
    }
}
```

### Integration testing

[](#integration-testing)

The plugin is compatible with the [Integration testing](https://book.cakephp.org/3.0/en/development/testing.html#controller-integration-testing) feature of CakePHP. You just need to follow the same directory pattern as you'd do for you application:

```
tests
  /TestCase
    /Controller
      /Posts
        /IndexActionTest.php

```

And a basic test file would look like:

```
// 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();
    }
}
```

Make sure you load the plugin in the `App\Application::bootstrap()` method, otherwise, Integration tests will not work. See the "Loading the plugin" section of this README to know how to.

### No-op methods

[](#no-op-methods)

As seen above, `Action` classes are instance of `\Cake\Controller\Controller`. Some methods in this class are related to actions. But since we are now having objects that represent actions, two methods had to be made "no-op" : `\Cake\Controller\Controller::isAction()` and `\Cake\Controller\Controller::setAction()`. Using these methods in an `Action` subclass will have no effect.

Configuration
-------------

[](#configuration)

### Strict Mode

[](#strict-mode)

As seen above, the plugin will let CakePHP handle the request in its regular dispatching cycle if an action can not be found. However, if you wish to only use `Action` classes, you can enable the strict mode. With strict mode enabled, the plugin will throw an exception if it can not find an `Action` class matching the request currently being resolved.

To enable the strict mode, set it using the `Configure` object in the **bootstrap.php** file of your application:

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

Contributing
------------

[](#contributing)

If you find a bug or would like to ask for a feature, please use the [GitHub issue tracker](https://github.com/HavokInspiration/cakephp-actions-class/issues). If you would like to submit a fix or a feature, please fork the repository and [submit a pull request](https://github.com/HavokInspiration/cakephp-actions-class/pulls).

### Coding standards

[](#coding-standards)

This repository follows the PSR-2 standard. Some methods might be prefixed with an underscore because they are an overload from existing methods inside the CakePHP framework.
Coding standards are checked when a pull request is made using the [Stickler CI](https://stickler-ci.com/) bot.

License
-------

[](#license)

Copyright (c) 2015 - 2017, Yves Piquel and licensed under [The MIT License](http://opensource.org/licenses/mit-license.php). Please refer to the LICENSE.txt file.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 89.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~21 days

Total

4

Last Release

3160d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c7ade5318f46ea6ffa30a140639a71bb9f078f8322efc24268d0f94cd7c4deee?d=identicon)[HavokInspiration](/maintainers/HavokInspiration)

---

Top Contributors

[![HavokInspiration](https://avatars.githubusercontent.com/u/5243386?v=4)](https://github.com/HavokInspiration "HavokInspiration (62 commits)")[![EdouardTack](https://avatars.githubusercontent.com/u/1247641?v=4)](https://github.com/EdouardTack "EdouardTack (7 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/havokinspiration-cakephp-actions-class/health.svg)

```
[![Health](https://phpackages.com/badges/havokinspiration-cakephp-actions-class/health.svg)](https://phpackages.com/packages/havokinspiration-cakephp-actions-class)
```

###  Alternatives

[cakephp/debug_kit

CakePHP Debug Kit

86514.0M138](/packages/cakephp-debug-kit)[cakephp/bake

Bake plugin for CakePHP

11211.2M158](/packages/cakephp-bake)[friendsofcake/bootstrap-ui

Bootstrap front-end framework support for CakePHP

3492.1M32](/packages/friendsofcake-bootstrap-ui)[cakephp/app

CakePHP skeleton app

3831.7M1](/packages/cakephp-app)[cakephp/localized

CakePHP Localized Plugin

218595.6k5](/packages/cakephp-localized)[cakephp/acl

Acl Plugin for CakePHP framework

109553.9k15](/packages/cakephp-acl)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
