How to Build a Custom Library in PhpFox?

  • Cubettech
  • Web App Development
  • 10 years ago

Building Custom Library in PhpFox
Custom Library allows extensions of applications without modifying the core files, it also caters the developers a flexibility to add code without interrupting any other application part. Custom library creations are much easier and is more flexible in that. Let us go through the various steps in building a custom library in PhpFox.

If you skim through the folder structure in you would come across the ‘include folder’. The ‘include folder’ is home for all libraries. The libraries in are tagged with specific names. Each of the library file should be tagged with a holding folder and the name of the holding folder should be the same as that of the name of the library class.

Now, let us start creating a file upload library in PhpFox.

On the very first step, we need to create a folder in the name of ‘fileupload’ under include. So our new folder structure will be include/library/phpfox/fileupload.

How to create a custom library?

Create our fileupload class file inside that.

 

<?php

defined('PHPFOX') or exit('NO DICE!');

/**

* A minimal fileupload implementation

*

* @package fileupload

*

* @author myname

*

* @version 1.0

*

*/

class Phpfox_Fileupload

{

}

Now we need to have some fileupload functions to handle the fileupload and save the details to database.

Lets have our basic constructor and a new handleFile() function.

function __construct()

{

switch ($_SERVER['REQUEST_METHOD'])

{

case 'POST':

$this->handleFile();

break;

default:

//handle the exception here.

break;

}

}

Now our constructor will check for the post method and will call our handleFile function

/***Handle File***/

protected function handleFiles($params)

{

//handle the posted files here

//call the service class to save the file details to db

}

These are all about a simple custom library. Now, let’s see how to make use of this.

How to call the custom library

Let’s make use of a simple jquery fileupload plugin for our example. Implement the upload UI in a new module call fileupload.

(Make sure that we have included the required jquery file upload scripts. Since we are discussing about how to make use of custom library, we are not going deep with the fileupload plugin)

$('#fileupload').fileupload({

url: path+'module/fileupload/static/upload.php',

});

Here, we are calling the upload.php file inside static fileupload module inside our core path.

Posting the upload file to the fileupload controller itself may not behave properly, since we have restrictions in calling the absolute path of the controller in the url param of the jquery.

Here in such cases we can make use of our static folder in fileupload library.

Create an upload.php file inside the static folder of fileupload library.

We may face some difficulties in accessing php library functions from static folder, because, we are not extending the properties like our controller and service files. Here, we need to sort out a way to access php library functions from static folder.

Accessing php library functions from static folder

The following piece of codes will help to include the PhpFox

/**

* Key to include phpFox

*

*/

define('PHPFOX', true);

define('PHPFOX_NO_SESSION',true);

define('PHPFOX_NO_USER_SESSION',true);

ob_start();

/**

* Directory Seperator

*

*/

define('PHPFOX_DS', DIRECTORY_SEPARATOR);

/**

* Root Directory

*

*/

define('PHPFOX_DIR', dirname(dirname(dirname(dirname(__FILE__)))) . PHPFOX_DS);

// Require Init

include PHPFOX_DIR .PHPFOX_DS.'include'.PHPFOX_DS.'init.inc.php';

That’s all, now we are free to call any objects here...

So just call over new fileupload library below the above piece of code as follows.

Phpfox::getLib('fileupload');

Done. The handleFile() function in this fileupload library will collect the post data from the URL and handle the rest of the things.

So, the canvas is ready, let’s colour the word of custom libraries by your own :).

Know More About This Topic from our Techies

Got a similar project idea?

Connect with us & let’s start the journey!

Questions about our products and services?

We're here to support you.

Staff augmentation is a flexible workforce strategy companies adopt to meet specific project needs or address skill gaps.

Begin your journey!
Need more help?