Wednesday, March 16, 2022

[FIXED] Where is the correct place to put vendor files in Cakephp 2.X?

Issue

I've taken over an old CakePHP 2.X website and have no prior experience with CakePHP so please forgive me if this is a stupid question.

I was looking at making some changes to some vendor files and noticed that we appear to have multiple copies of various files (which are, for the most part, identical) in 2 different places:

  • app/webroot/api/vendor/API/lib/
  • vendors/API/lib/

Additionally I noticed there are several other vendor directories in various other places.

I am using App::import('Vendor', 'example', array('file' => 'API/lib/example.php')); to load the scripts in question.

Could someone please explain to me what the best practices are regarding file structure relating to vendor files? Additionally, am I safe to delete the duplicate copy of all the files? How does CakePHP know which copy to load?

Edit:

I have come to the conclusion that the files are being loaded from vendors/API/lib/ rather than app/webroot/api/vendor/API/lib/, is it possible that the files in the latter location are redundant? I cannot seem to find any references to them.


Solution

Well as Sudhir has commented you, there is a folder in your app project which is called Vendor. I would recommend you to put it there. app > Vendor

For example, I have created a folder called Csv for generating my own csv files through a Shell which is launching them. It is located inside app > Vendor > Csv

For importing this to my projects, I did the next for being able to use it:

     <?php

      include('GenericShell.php');
      require_once(ROOT . DS . 'app' . DS . 'Vendor' . DS . 'Csv' . DS . 
      'CsvGenerator.php');

     class CsvPatientsShell extends GenericShell {

That's one only example with PHP.

One other one would be, if in this case you have a Component which is called component.php and you want to import it to a Controller which you use frequently inside your project :

Component would be located into Controller > Component > Namecomponent.php

The next thing you would have to do would be to do the import likewise inside your controller: Let's say your controller's name is NameController.php and is located inside the Controller folder. Controller > NameController.php

  public function main_function() {

    App::import('Component', 'Namecomponent');
    $NameComponent = new NameComponent();

    $this->layout = null;
    $this->autoLayout = false;
    die();
}

That would be a more correct way to do it with CakePhp but both mentioned are legit I'd say. I hope that will help you somehow.



Answered By - Oris Sin

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.