Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to import vendor files in CakePHP 3 ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 6.63k
    Comment on it

    Importing vendor files in Cakephp 3

    If you have used Cakephp 2x then we know how to import vendor files there. In Cakephp 2x we do the following to import vendor files:

    App::import('Vendor', 'Twitter', array('file' => 'Twitter' . DS . 'src'. DS. 'Twitter.php'));

    In order to load vendor files in Cakephp 3 you need to load the files using require.

    Steps to import vendor file:

    i) Place library folder under your_project/vendor/

     

    ii) include library file using require_once(ROOT . DS . 'vendor' . DS . "my_library_folder" . DS . "my_library_base_class.php") , this includes the library code file in our code.

     

    Example: Please see the sample example below:

     //The following line should do the same like App::import() in the older version of cakePHP
     require_once(ROOT . 'vendor' . DS  . 'Twitter' . DS . 'src' . DS . 'twitter.php');


    iii) Include class name in top of Controller like :

    namespace App\Controller; use MyLibraryBaseClass;

     

    This will going to import the library code in our namespace to be used.

     

    iv) Create object of loaded class as $my_obj= new MyLibraryBaseClass();

     

    Example: This is the example for loading the class twitter, it was included above on point number (ii)

     

     $tiwiterApi = new twitter();

     

    Thats all done!

     

    Thanks for reading the blog.

     

    How to import vendor files in CakePHP 3 ?

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: