Creating Actions

Now that you successfully setup FRAPI (Both the admin and the frontend side) you must create your first action. As the Running on nginx and the Running on Apache pages refer to the administration interface url as http://admin.frapi, we'll use that URL in this example.

Accessing the Admin Interface

Firstly you must go to the admin interface and login. The default username is: admin and default password is: password.

Once this is done click on "Actions"

and you will be directed to this screen

and as the arrow suggests, click on "Add new Action".

Using the Admin Interface

The following screen will then be displayed, prompting you for the information necessary to create your own action.

The fields displayed on this screen are described below.

Name: This is the name of the action you are creating (The file with code that will be generated with that name).

Enabled: This indicates if the action you are creating should be enabled and available for someone to access.

Public: This indicates if the action you are creating is publicly accessible. If you do not check this box then only partners will be able to access the action with a valid username and password.

Custom Route: This is a very simple routing system. It allows you to modify the url of the action. Currently you can have a / delimited url. If you prefix part of the url with a : it will become a parameter for the action. For instance if the custom route for your action /user/:user_id and it was called like so: http://api.frapi/user/2 then inside your action the $user_id variable would be 2.

Description: This is the description of your action. FRAPI has an API documentation generation system and when generating API documentation, it uses the description you put for the action to describe it. This generated documention can be used by third party developers that are using your API.

Adding Parameters

Below the simple admin interface for adding actions you will notice the Add Parameter section.

To start adding parameters click on the Add Parameter link. This will display the parameter name field and well as a check box to indicate if the parameter is required.

This is used both in the code generation (automatic parameter verification in the API frontend) and in the generated API documentation. For each time you click Add Parameter a new set of fields will be added allowing you add as many parameters as you would like. An example use of parameters would be if you required a username and password for use with your own ACL.

Once you are have completed editing your action and add parameters click Save. If all went well you should see a message that states "Action {NAME} Added"

Generating the Action Code

Now that the action is created, you need to synchronize the codebase (This means that FRAPI will generate the skeleton code for your new actions). If you look at the top right of your administration interface you should see a "Sync Now" button.

When you click on that button FRAPI grabs all the actions you created and generates the skeleton for the actions in the code base.

When it's done synchronizing, you will get a message that says "Development environment has been sychronized" and if you go to your FRAPI "custom" directory (Which is located in FRAPI_PATH/src/frapi/custom) you will see a folder called "Action" which will now contain the action you just created/synchronized.

Example

Now that you have learned how to create an action, let's get to the fun stuff and look at an example. This example will show you how to create two actions and the Coding Actions section will teach you how to write the code.

Firstly let's start with our database, below is SQL to create the table we will be manipulating in MySQL. Feel free to modify as necessary for your database of choice.

Now we need to create two actions. One will be for handling the PUT action, the other for GET, POST, DELETE and HEAD. The reason this is necessary is that FRAPI's routing system cannot handle a route that has an optional argument. Since the PUT action will not require a user id it needs to be created seperately.

First up is our PUT action. Note the custom route does not contain a :user_id on the end. The : is how we define variables in our custom routes.

Once you have the above data entered, click "Save" to save your action.

Next up is our action to handle GET, POST, DELETE and HEAD. Since all of these methods require a user id to function properly we add :user_id to the end of our custom route

Once you have the above data entered, click "Save" to save your action. Then "Sync Now" to have FRAPI generate the skeleton code in your FRAPI_PATH/src/frapi/custom/Action directory.

Now that your skeleton code has been created please proceed to the Coding Actions section for details on how to code the actions you just created.