16

I'm working with CakePHP 3(beta 2) version recently launched. I need to integrate Facebook Login using PHP SDKs and I'm not clear with importing vendor files in this version.
In CakePHP 2x, I had used

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

So I need to reproduce the same in CakePHP 3x(I'm not using composer).
Any reference for this?

2
  • 1
    Adding my two cents. Ideally vendor files should be autoloaded with Composer, if you have vendor files that cannot be autoloaded or installed with Composer you will need to use require to load them. Therefore @Ayman-b 's answer should be accepted as it comes for CakePHP-3.0. Commented Oct 26, 2014 at 1:31
  • If possible, it will be better to use Composer. Life will be easier for both yourself and other devs who need to keep all dependencies up-to-date. Commented Feb 11, 2016 at 1:50

5 Answers 5

25

Well you will have to load it yourself if composer is not an option. You can always use the very basic require method and create a new instance of the vendor class yourself. Reference: http://book.cakephp.org/3.0/en/core-libraries/app.html#loading-vendor-files

Use:

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

 $facebookApi = new facebook();
5
  • 1
    I like your comment way better, it provides more contextual info :)
    – ndm
    Commented Oct 25, 2014 at 15:38
  • Lool :) Agree ... Added it to the answer Commented Oct 25, 2014 at 15:45
  • 2
    APP gives path to src folder of cakephp3. and 'vendor' is not a subfolder of 'src' folder anymore.So require_once will not work in your code.
    – Dashrath
    Commented Jul 7, 2015 at 16:23
  • You are right @Dashrath I just edited my answer and replaced it with ROOT Commented Jul 8, 2015 at 11:31
  • Can i use this require_once(ROOT . 'vendor/Facebook/src/facebook.php'); ?
    – Pradeepta
    Commented Mar 1, 2017 at 5:04
20

In cakephp3 , to add a new vendor library you can follow below steps :

  1. Place library folder under your_project/vendor/
  2. 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.
  3. Include class name in top of Controller like :

namespace App\Controller; use MyLibraryBaseClass; ,

this imports the library code file in our namespace to be used.

  1. create object of loaded class as

    $my_obj= new MyLibraryBaseClass();

3
  • If this still doesn't work for you, try adding a backslash eg. $my_obj= new \MyLibraryBaseClass();.
    – Cellydy
    Commented Jan 13, 2017 at 12:30
  • It show me 2 errors: 1 /PHP Notice: Use of undefined constant ROOT - assumed 'ROOT' in C:\xampp\htdocs\ProjectX\webroot\classX.php on line 6 . 2/Notice: Use of undefined constant DS - assumed 'DS' in C:\xampp\htdocs\ProjectX\webroot\classX.php on line 6 Commented Jun 20, 2017 at 13:21
  • This should be the answer! Commented Nov 13, 2017 at 7:33
2

the answer provided by Ayman B. does not look like doing the job as expected in the question after i tried it myself , for the following reasons :

  • the vendor folder in cakephp3 is not located in src folder under APP namespace , it is moved to the ROOT folder , by doing you will not be able to load your Facebook class as expected , try it yourself and you will see the result ...
  • By loading a vendor file this is does not automatically load the class name itself , if your vendor lib does not follows the follwing rule as PSR-0 rule : \VENDOR\PACKAGE\TEST.CLASS.PHP and inside the test.class.php there is not a class definition that must be called or imported in your script with a defined namespace keyword in the begining of this script as follows : namespace then the code above will not work

To correct the answer you have to do some several steps as follows :

1 - Define in bootstrap.php a new cakephp constant like so : define('VENDOR',ROOT . DS . 'vendor' .DS); as VENDOR constante is removed in cakephp 3.x you can define it yourself 2 - After that , you have to specify the vendor name , the package name and the class name in a vendor constante like : define('_',; and then you can do $facebookApi = new \\();

this is will work for you as expected in the question

If you have issues try to get back to me , i will show you an example of use as described here ...

1

I also had the same problem with CakePHP 3.0.

Do the installation as instructed using Composer.

Then You have to properly load the plugin in your controller with the use statement. Like this:

use Ghunti\HighchartsPHP\Highchart;

  • If you're using the plugin in most of the pages, instead of loading in each Controller, you can add the same line in your bootstrap.php file, right below the other use statements.

That will solve the problem of using the plugin.

0

As of CakePhp 3.x, the recommended code standard is to use require_once without the brackets"()".

require_once(ROOT.'Folder'.DIRECTORY_SEPARATOR.'requiredfile.ph');

becomes

require_once ROOT.'Folder'.DIRECTORY_SEPARATOR.'requiredfile.ph';

https://book.cakephp.org/3.0/en/contributing/cakephp-coding-conventions.html

Hope that helps someone in the future.

2
  • This is not an answer to the question. If this was meant as a comment to the answer of Ayman B.: You have to earn a reputation score of 50 first to leave comments. You can improve your reputation by asking or answering questions: stackoverflow.com/questions.
    – Leviathan
    Commented Mar 26, 2017 at 23:22
  • It show me error: 1 /PHP Notice: Use of undefined constant ROOT - assumed 'ROOT' in C:\xampp\htdocs\ProjectX\webroot\classX.php on line 6 . Commented Jun 20, 2017 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.