Download the PHP package corneltek/actionkit without Composer
On this page you can find all versions of the php package corneltek/actionkit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package actionkit
ActionKit
ActionKit is a library that let you share the business logics across controllers, pages, ajax requests.
Sometimes, you need to reuse code across your controllers, pages, ajax requests, you might sit down and write a shared controller class to share the common code for reuse. This approach might work well for small applications, , however, when your application is getting bigger and bigger, it will be very complex to share the common code, and hard to maintain.
ActionKit provides a way to wrap your common code up, and make these common code reuseable in everywhere in the application.
Besides of sharing the logics across your controllers, you may also define the parameters with types, validators, form widget type and a lot of parameter options, and render your Action as a web form.
[Web Form] => [Input: paramters] => [ Parameter Validation ]
=> [Execute the logic in Action]
=> [Return result: Success or Failure, Data: processed data]
=> [Render result on the web page]
Hence, you don't need to handle the ajax mechanisums, controller handlers, parameter validations, ActionKit\Action does all the jobs for you automatically, so you can focus on the core logics that you only need to handle.
Action is just like API (application programming interface), which can be triggered from HTTP requests, Ajax requests, or from backend, here is the work flow:
A Basic Action
A minimal action skeleton:
To use Action, you should define a run
method at least,
in this run
method, you write your logics, operations,
then return the result at the end.
To report success result, you can simple use success
method:
You can also pass data to the action result, by appending another argument in array:
To report error:
Action Signature
To trigger an action from front-end, you can define an action signature in your HTML form.
When submitting this form, ActionRunner uses this signature to dispatch your action to the right place.
The convention rule is like below:
- A class with namespace like
App\Action\CreateUser
will be converted to signatureApp::Action::CreateUser
.
A Simple Action Skeleton
Then the caller:
To take an action, simply call invoke
method to trigger
the action.
the invoke
method trigger runPreinit
, runInit
,
beforeRun
, run
, afterRun
in order.
Action Schema
run method
methods
methods that you will use in run
:
-
$this->arg(string $key)
: get argument from action by key. -
$this->setArgs(array $arguments)
set action arguments. -
$this->success(string $message, $data = array())
Report success message.
You can also pass data to the action result object.
By using this method, action object creates an action result object, and register the result object into the Action Result Pool by using the action signature as a key.
@see ActionKit\Runner
-
In Ajax mode, the action result will be converted into JSON format, and the front-end
Action.js
will get the action result data, then display the result message (or dispatch to jGrowl plugin) -
In HTTP POST/GET mode, the action result is also saved in the Action Result Pool, and by calling a simple twig macro, you can render these action result objects into HTML format string.
@see bundles/CoreBundle/Templates/phifty/action_result.html
-
-
$this->error(string $message)
Report error message.
properties
properties that you will need in Action run
method:
-
request: HttpRequest object, you can retrieve POST, GET, SESSION, SERVER through a simple API.
$this->request->param('user') // is equal to isset($_REQUEST['user']) ? $_REQUEST['user'] : null $this->request->server->HTTP_HOST // is equal to isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null
Action param methods
Action Result
After executing an action, the action creates an action
result object inside itself, you can retrieve the action result
object through the getResult()
method of action object
to see if it's successfully executed or encountered an error.
Every action result object is saved in the ActionRunner instance. (the action result pool, which is a singleton object)
You can also fetch action result objects from ActionRunner.
An ActionResult
object contains a flag (success or error),
a message string, a data stash.
Here is a simple example to check the result error:
To get an action result from an action object.
To get an action result from ActionRunner:
RecordAction
Record Action is very useful for connecting ORM with front-end form, Record Action passes arguments to model object and validates the arguments from HTTP request.
If all validation passed, and no PDOExcetion was catched, the action result will be generated and ready to send back to front-end.
There are 3 type record actions, which is mapped to CRUD operations:
- Create
- Update
- Delete
The mapped action classes are:
- CreateRecordAction
- UpdateRecordAction
- DeleteRecordAction
Those 3 record action classes inherits BaseRecordAction class.
BaseRecordAction class provides most methods for glueing ORM interface methods and the result data conversion.
RecordAction Synopsis
Messages
The BaseRecordAction
provides the default message interface,
to override these messages (for both success and error
message) you can simply override the methods:
RecordAction Examples
CreateNews
UpdateNews
Record Action API
RecordAction schema methods
- useRecordSchema
Record Action Generator
CRUD Actions could be automatically generated, or be manully created by hands.
To generate CreateRecordAction from a model class name
To generate UpdateRecordAction from a model class name
To generate custom action:
Or even shorter (???):
Or create record actions from record object:
Action Widget
Action widgets depends on the parameter definition, the default widget type is TextInput.
In action schema, the parameters you defined can generate form widgets (with FormKit) automatically.
What you only to do is to define a renderAs
attribute for
your parameters in your action schema.
For example:
And then, to get the form widget through Action object, you can do:
And to render it:
For other type widgets, like SelectInput you can specify
options
:
You can also force a form widget type for widget method, which will override the widget type that you defined previously:
Action View
An action view may contains a formkit layout builder, but an action view build everything for you.
to create an action view, you can simple calls the createView
method
Action rendering throught built-in StackView
By using ActionKit StackView, you don't need to write HTML, the form elements are automatically generated.
Here is a StackView synopsis:
Use case:
And you can render action view via Action's asView
method:
So that if you're in Twig template, you can do:
You can also pass extra options to View class:
Action Rendering (render by pure HTML elements)
You can simply render a HTML form to trigger corresponding
action class, in this example we trigger the
User\Action\UpdateUser
action, which is generated
automatically through the Dynamic Action Generator.
Action Rendering and Action.js integration
<script>
$(function() {
Action.form( $('#profile')).setup({
validation: "msgbox",
status: true
});
});
</script>
{{ Web.render_result( update.signature ) |raw}}
{{ update.asView('ActionKit\\View\\StackView',{
'form_id': 'profile'
}).render() |raw }}
</div>
Action Rendering (render field by field)
In controller, you can initialize a action object:
Then in template, you can call action API to render these
fields by these methods, eg renderSignatureWidget
,
renderWidget
, renderLabel
, renderSubmitWidget
..etc:
Front-end Action API
You can execute Actions from front-end, it's more like an API. to send action to execute, you need to include action.js from action assets.
action.js provides a short helper named runAction
that helps you to execute Action, you can
call runAction function in following forms:
runAction( {Action Signature}, {Arguments});
runAction( {Action Signature}, {Arguments} , {Options});
runAction( {Action Signature}, {Arguments} , {Options}, {Callback} );
runAction( {Action Signature}, {Arguments} , {Callback} );
runAction( {Action Signature}, {Callback} );
runAction( {Action Signature} );
And in the below example, we send Stock::Action::DeleteTransaction
to backend with a record id
to delete a transaction record, if it's successful, then fade remove the elements from HTML.
All versions of actionkit with dependencies
corneltek/fileutil Version ^1.7
corneltek/universal Version ^1.8
corneltek/imagekit Version ^1
corneltek/class-template Version ^3.0.0
corneltek/formkit Version ^1
phifty/locale Version ^3
twig/twig Version ^1
pekkis/mime-types Version ^1
pimple/pimple Version ^3.0