Reputation: 788
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:
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.
Upvotes: 2
Views: 538
Reputation: 788
Coming back a few years later with a little more experience, I feel like I can answer this question better and hopefully help others.
There are only 2 vendor folders in a standard CakePHP 2.0 implementation:
/vendors
and /app/Vendor
(Note the difference in case and plurality). The one I found in /app/webroot and any other vendor folders you may find are not associated with cakePHP.
The reason CakePHP has 2 different vendor folders is because you are able to run 2 (or more) CakePHP apps on one copy of the library by creating a second app folder. The /vendors
folder is for vendors that are common between all these apps and the /app/Vendor
folder is for vendors of that specific app.
I could not find any best practice information on which one to use by default but I suggest you pick one and stick with it unless you anticipate adding a second app to the installation in the future.
By using only these 2 folders for vendors you can ensure that standard CakePHP helper methods such as "App::import" and "App::uses" are able to find them.
Upvotes: 0
Reputation: 1238
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.
Upvotes: 1