Fork me on GitHub

Nibble Development

Nibble Development

PHP classes and libraries for developers

Working in the Actions class

In the last chapter creating a method was covered in basic detail. As a recap, below is the example given showing a method index created in the bites/frontend namespace:

The Actions class is essentially the Controller when looking at the Nibble PHP Framework as a MVC framework. However currently there are no real model files so interactions with RedBeanPHP are also done in the Actions class.

Using RedBeanPHP in the Actions class

To use RedBeanPHP in the Actions class is simple. RedBeanPHP methods are all called using the class “R” and are all static. This means that a simple SQL statement can be executed using the command:

More complex calls to R can be made, in fact all of the methods and functionality described in the RedBeanPHP documentation is available to use in the Actions class. This means that, for example, setting up a new user with the user Bite is as simple as:

Using Nibble Forms and Flash classes in Actions

The ActionsBase class, the parent of all Actions classes, instantiates the flash class. This means that the flash class methods are available through the class object “flash”:

Nibble PHP Forms are accessed through the Render class, the ActionsBase class also instantiates the Render class. This means that forms can be accessed like so:

Where YourFormName is the name of the form file, in the current Bites form directory, without the extension. Thus a login.php file would be called:

The Flash class has all of the scripts set up for it meaning that you can use the message() and flashMessage() methods out of the box. The message() method should be used when the user needs to be messaged in the view loaded by the current action method. The flashMessage() method should be used when the user is to be messaged in the following view.

Adding variables for the view

Inspired by Anant Garg’s build your own MVC framework the Render class uses the PHP method “extract” to extract the class array “variables” to each view and partial rendered. Values are added to “variables” using the PHP magic method “__set” in the ActionsBase class. This means setting variables for the view is as simple as setting a previously unset property in the Actions class:

Variables set this way do not just have to be string values, they can be arrays, objects or any other PHP variable type. A common use may be to send arrays of Beans from the RedBeanPHP ORM so that the view can loop through them and display the contents to the users.

Luke Rotherfield

Freelance PHP Developer
is a PHP developer dedicated to writing the most awesome liraries for his fellow devs :) enjoy

Latest posts by Luke Rotherfield (see all)

Leave a Comment