core PHP file use as library in CakePHP

This post explains how use core PHP file as library file in CakePHP. I have requirement that use external file need to use in CakePHP application. Earlier post we learned how to create library files in CodeIgniter. Now we can learn in CakePHP. In below I will explain how to do with CakePHP.

core PHP file use as library in CakePHP by Anil Kumar Panigrahi

core PHP file use as library in CakePHP by Anil Kumar Panigrahi

Step 1:

Create / Download existing core PHP file eg:PhpClass.php

class PhpClass{
    /**
     * Get the current script version.
     * This is useful for the PhpClass.php file,
     * for email gavatar .
     *
     * @return url of gavatar image.
     */

   public function get_gravatar( $email, $s = 25, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
        $url = 'http://www.gravatar.com/avatar/';
        $url .= md5( strtolower( trim( $email ) ) );
        $url .= "?s=$s&d=$d&r=$r";
        if ( $img ) {
            $url = '<img src="' . $url . '"';
            foreach ( $atts as $key => $val )
                $url .= ' ' . $key . '="' . $val . '"';
            $url .= ' />';
        }
        return $url;
    }
}

Step 2:

Save that file in below path

/cakephp/app/Vendor/CorePHP/PhpClass.php

Step 3:

In your controller include this file and call the method

// include a core PHP file
App::import('Vendor', 'CorePHP/PhpClass');
// creating object for that class
$obj = new PhpClass;
$imgUrl = $obj->get_gravatar($this->Auth->User('email'));
$this->set('imgUrl', $imgUrl);

It is explained with simple steps to core PHP file use as library in CakePHP. You can take any other file and work with that.

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online ... Contact : [email protected]

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *