For smart Primates & ROBOTS (oh and ALIENS ).

Blogroll

Wednesday, April 08, 2015

How to create own custom class in cakephp 3.0

Problem:

How to use own custom class or library class in CakePHP 3.0
If you have own useful library and you want to use it in your CakePHP 3.0 MVC Framework then follow below steps.


Solution:

If you have good knowledge of Core PHP then It will be easy to create own custom class in CakePHP 3.0 and use it. follow below steps:

1# Create the folder "MyLib" at root of CakePHP 3.0 structure where bin,config,logs folders are showing. you can also create it in the "vendor" folder but I think we should not create in there.

2# Create "UtilClass.php" file in the "MyLib" folder.

3# And last use it in your controller with it's path.

Example:

1# Create the folder "MyLib" at root of cakephp 3.0 structure where bin,config,logs folders are showing.

2# Create "UtilClass.php" file in the "MyLib" folder with below code:
<?php
namespace MyLib; //it is important,it is folder name
class UtilClass
{
    public function show()
    {
        return 'I am from my custom class in cakephp 3.0';
    }
}
?>


3# Use is as below code in any controller:
<?php
use MyLib\UtilClass;
 class TransportersController extends AppController
 {
     public function index() {

    }

    public function showtransporter(){
      require_once(ROOT .DS. "MyLib" . DS . "UtilClass.php");
      $util_obj = new UtilClass();
      echo $util_obj->show();

  }
}
?>
Share:

0 comments:

Post a Comment

Unable to resolve <website url> Try checking the for validity Google Pagespeed Insights

Unable to resolve <website url> Try checking the for validity Google Pagespeed Insights   You have a website and want to test it'...

Ads Inside Post

Powered by Blogger.

Arsip

Blog Archive