Helpers

You only need to load the Helper classes when using them from your controllers, models or libraries.

However, they are autoloaded by default in the view files using the alias names specified in the config.php file.

All helper classes are accessed statically so you do not need to create an instance to access the methods.

In order to access a helper method specify the helper class name, the scope resolution operator and then the name of the method:


Redirect::to(); 

To use a helper class in your controller, first require it by specifying the full namespace.


use Gliver\Redirect\Redirect;

You can also alias the helper class name to a name of your choice:


use Gliver\Redirect\Redirect as Red; 

Then use the Red class to access all the Redirect helper methods.


<?php namespace Controllers;

use Gliver\Redirect\Redirect; 

class HomeController extends BaseController {

    public function goThere()
    {
        $path = "...";

        Redirect::to($path);
        
    }  

}
 

Array

The ArrayHelper class enables you to create arrays out of string as well as manipulate arrays.

This class allows for method chaining, so you can chain all the methods you would like to use in one method call and get the final result returned.

Here are the methods available with the ArrayHelper class.

ArrayHelper::parts

This method splits a string into a numerically indexed array depending on the delimiter provided.

It expects three parameters:


ArrayHelper::parts($delimiter = null, $string = null, $limit = null)->get() 

$delimtier is the character(s) to use as a separator for exploding the input string into an array.

$string is the input string which is to be exploded into an array

$limit is an integer value that limits the number of elements to return

Example:


$name_string  = "This is Gliver Documentation";

$name_array = ArrayHelper::parts(' ', $name_string)->get();

//the final value of the $name_array is this
$name_array = array(
    0 => 'This',
    1 => 'is',
    2 => 'Gliver',
    3 => 'Documentation'
);

If we only wanted the first two elements to be returned, we would pass 2 as the third parameter so that the parts after 'is' are not returned.

ArrayHelper::join

This method joins an array into a string based on the joining parameter provided

It expects two parameters:


ArrayHelper::join($glue = null, array $inputArray = null)->get();  

$glue The string to use to join the array elements into string

$array The string which is to be exploded into an array

Example:


//input array
$names = array(
    0 => 'Scott',
    1 => 'Mendoza',
    2 => 'Stuart'
);

$names_string  = ArrayHelper::join(', ', $names_array)->get();

//the final value of $names_string becomes
$names_string = 'Scott, Mendoza, Stuart';

ArrayHelper::clean

This method loops through the items of an array removing elements with empty or null values

It expects one parameter:


ArrayHelper::clean($array = null)->get();

$array The array whose values are to be cleaned

Example:


//input array
$user_info = array(
    'name' => 'Bernhard Anaurdis',
    'gender' => 'male',
    'address' => null,
    'age' => 78
);

//remove empty and null values
$user_info = ArrayHelper::clean($user_info)->get();

//the final content of $user_info
$user_info = array(
    'name' => 'Bernhard Anaurdis',
    'gender' => 'male',
    'age' => 78
);

ArrayHelper::trim

This method loops through array elements removing whitespaces from begining and ending of string element values

It expects one parameter:


ArrayHelper::trim($array = null)->get();  

$array The input array to be trimmed of whitespace

Example:


//input array
$user_info = array(
    'name' => '  Sagini Obed  ',
    'status' => ' Will tell you    when   I see u',
    'address' => '4399  6th Street    Benton Harbor  '
);

//get trimmed user_info
$user_info = ArrayHelper::trim($user_info)->get();

//final content of $user_info
$user_info = array(
    'name' => 'Sagini Obed',
    'status' => 'Will tell you    when   I see u',
    'address' => '4399  6th Street    Benton Harbor'
);

ArrayHelper::flatten

This method converts an associative array into a numerically indexed array

It expects two paramaters:


ArrayHelper::flatten($array = null, $return = array())->get(); 

$array The array to flatten

$return The return array

Example:


//input array 
$array = array(
    'first_name' => 'Vince',
    'middle_name' => 'Fargo',
    'last_name' => 'Lombardi'
);

$array = ArrayHelper::flatten($array)->get();

//final content of the $array
$array = array(
    0 => 'Vince',
    1 => 'Fargo',
    2 => 'Lombardi'
);

ArrayHelper::first

This method returns the first element in an array

It expects one argument:


ArrayHelper::first($array = null)->get(); 

$array The array whose first element is to be returned

Example:


//input array
$cities = array('New York', 'New Hamshire', 'New Jersy');

$city = ArrayHelper::first($cities)->get();

//content of city
$city = array('New York');

ArrayHelper::slice

This method splits an array and returns the specified section.parts

It expects four parameters:


ArrayHelper::slice($inputArray = null, $offset = null,  = null, $preserveKeys = false)->get();

$inputArray The input array that is to be split and parts returned

$offset The int to specify where to start truncating from

$length The int to specify the number of elements to return

$preserveKeys boolean true|false Set to true to preserver numberic keys, otherwise would be reindexed

Example:


//inpur array
$employee == array(
    0 => 'Software Engineer',
    1 => '$47,000',
    2 => 'Marcial Araujo',
    3 => '6th Street Benton Harbor, MI',
    4 => 'female'
);

$employee = ArrayHelper::slice($employee, 2)->get();

//final content of $employee
$employee = array(
    0 => 'Marcial Araujo',
    1 => '6th Street Benton Harbor, MI',
    2 => 'female'
);

ArrayHelper::KeyExists

This method checks if an array key exists

This method expects two parameters:


ArrayHelper::KeyExists($key = null, array $inputArrayToSearch = null)->get();  

$key The key to search for inthe input array

$inputArrayToSearch The array to check against

Upload

This helper class performs file uploads on the server.

Upload::doUpload

The Upload helper class has one method which takes three parameters:


Upload::doUpload($file_name, $target_dir = null, $file_type = null); 

$file_nameThe name of the file to upload as submitted in the form

$target_dirThe name of the directory where to upload the file to.

If you leave this option or set it to null, the file would be uploaded to the default upload directory specified in the config.php file.

In order to specify a different directory, you give the relative path to the folder including the trailing forward slash as:

public/uploads/profilepics/

$file_type The file type to be checked for validation.

If you would like to only upload image files, set this option to 'images' so that the uploaded file is checked for valid image format.

This method returns a Helpers\Upload\UploadResponseClass object with data about the performed upload and the property for checking if the upload was successful or unsuccessful.

Example:


//lets  write a simple form for uploading image file
<!DOCTYPE html>
<html>
<body>

<form action="http://localhost/myapp/home/upload" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="gravator" id="gravator">
    <input type="submit" value="Upload Profile Image" name="Upload">
</form>

//let's write some code to upload this file after submission

use Helpers\Upload\Upload;

class HomeController extends BaseController {

    public function postUpload()
    {   
        $upload = Upload::doUpload('gravator', null, 'image');

        print_r($upload);

        //the object printed would have this structure
        Helpers\Upload\UploadResponseClass Object
        (
            [success] => //1 if upload was successful
            [upload_path_full] => //full path to the uploaded file
            [upload_path_relative] => //relative path to file from root if your installation
            [file_name] => //name of the file before uploading, different from final file name
            [file_size] => //interger representing the size of the file
        )

    }
    ...
}

</body>
</html> 

Url

This helper class resolves urls and returns the appropriate url string required

To load this class use:


use Gliver\Url\Url; 

Url::link

This method returns the base url string - the url to your root installation

It does not expect any parameter

It returns the url string

Example:


//say you installed gliver in 'myapp' folder in your localhost

$base_url = Url::link();

echo $base_url; //should output http://localhost/myapp/

Redirect

This helper class helps you to manage php session re-direction

You load this class in this manner:


use Gliver\Redirect\Redirect;

This class sends headers back to the browser and returns a REDIRECT(302) status code to the browser.

Redirect::to

This method redirects the browser to the url specified

It can take two parameters:

 
Redirect::to($path, array $data = null);  

$path The controller or route name to redirect to. This parameter is required.

$data The data to append to the url as query string. This has to be in a valid array format. This parameter is optional.

Example:


//say we would like to redirect to the ProfileController view() method with user id information
//lets compose the data array
$data = array('id'=>9897717171);

//this loads this url http://localhost/gliver/profile/view?id=9897717171
Redirect::to('profile/view', $data); 

Redirect::with

This method enables you to specify parameters to be set as part of the redirect url.

These parameters will be appended as a query string to the url string generated.

It expects one parameter - an array with the key/value pairs to be appended to url.

Array keys with null or empty elements will be stripped off from the query string.

$data The array with parameter to pass to url as query string. This parameter is required.

You call this method chainied with the to() method in this manner:


Redirect::with($data)->to('home'); 

Calling this method is the same as passing the $data array as a second parameter to the Redirect::to() method

Example:


//say we would like to redirect to the ProfileController view() method with user id information
//lets compose the data array
$data = array('id'=>9897717171);

//this loads this url http://localhost/gliver/profile/view?id=9897717171
Redirect::with($data)->to('profile/view');