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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

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

Step 3:

In your controller include this file and call the method

1
2
3
4
5
6
// 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.


0 Comments

Leave a Reply

Avatar placeholder

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