Problem
I am trying to use the PayPal Checkout REST SDK which requires the PayPal library to be autoloaded through composer. I have gone through the steps to enable Composer in CodeIgniter 3 but when I go to my controller where I am autoloading the PayPal\Rest\ApiContext class I get the following error:
Fatal error: Class 'PayPal\Rest\ApiContext' not found in
C:\xampp\htdocs\php\toucan-talk\App\modules\paypal\controllers\Paypal.php
on line 15
What I have so far
Here is my composer.json file
{
"require": {
"paypal/rest-api-sdk-php" : "*"
}
}
I have set $config['composer_autoload'] = TRUE; in my config.php file.
Here is my controller
<?php
use PayPal\Rest\ApiContext;
class Paypal extends MX_Controller
{
public function __construct()
{
$api = new ApiContext(
);
var_dump($api);
}
}
Question
How do I troubleshoot composer and its autoloader so that I can pinpoint where the autoload process is failing.
Well here is the solution: in config.php instead of setting...
$config['composer_autoload'] = TRUE;
You need to put
$config['composer_autoload'] = FCPATH . 'vendor/autoload.php';
however I am still not certain why this works as opposed to the original documentations recommendation. Bit of a headache really.
I know this is quite an old Question but still, i want to put my input that basically CodeIgniter 3 specifically 3.1.6 have problem that when you do
$config['composer_autload'] = TRUE:
then it won't work and doesn't autoload the files from vendor folder now there are 2 possible solutions for this one solution already mentioned by "Jethro Hazelhurst".
The other simple solution is that copy composer.json inside application folder and then run composer install from the application folder or just move vendor folder along with composer.json and composer.lock to the application folder and then this TRUE value will work properly.
Thanks.
You can use composer dump-autoload in command prompt. This will regenerate list of files to be included in the project.
For anybody else that might have this issue.
Codeigniter 3 has third_party folder inside application, use that for putting vendor directory (vendor directory contains the libraries).
composer.json (/composer.json)
tell composer to use the vendor directory inside third_party folder to store libraries
"config": {
"vendor-dir": "application/third_party/vendor"
},
config.php (/application/config/config.php)
Tell Codeigniter 3 to autoload all libraries from third_party/vendor folder
$config['composer_autoload'] = APPPATH . 'third_party/vendor/autoload.php';
PS: Autoloading All Libraries On Every Request could result into a slow website if there are many dependencies and a moderate to heavy traffic site. Load libraries as needed if that is the case.
Related
I'm trying to make a composer package, but I'm struggling to setup the autoload to work in the project where I'm developing it.
I have two projects:
Project Foo (where I'm developing the package).
Project Bar (where I've installed the package: composer require myhandle/mypackage).
In Project Foo I (obviously) have to be able to use the package as well. But setting up the autoload in my own package is not working.
However... When I commit the changes to Github, update the package at Packagist and run composer update in Project Bar, then it works(?!).
And wierdly enough... If I do composer update from Project Foo (the project where it isn't working). So updating the package to it's current version (?), makes it start working.
So it must somehow be related to how I've setup the autoload.
A note:
I started the package by making a new (empty) folder in the vendor directory, and then build the package there. I figured it was smart, since it then would mirror how it would look had I composer required the package.
I don't know if this is bad practice, since Composer no longer 'are in control' of all files in the vendor directory.
There are tons of guides on how to make a composer package out there, - but non of them explains about a good way to structure the files in the project where the package is being developed.
Here's what I do to 'get the error' (in Project Foo):
Create new class file, such as: myhandle/mypackage/src/Test.php
Then I instantiate it like this: $test = new MyNamespace\MyPackageName\Test();
And then I get the error:
Fatal error: Uncaught Error: Class 'MyNamespace\MyPackageName\Test' not found
And this is what works in Project Bar (the very same code).
I can't find a guide on how to correctly setup autoload in the package I'm developing. I'm using this autoload file, that I found in another composer project. I've put it in the root of my project. It looks like this:
<?php
namespace MyNamespace\MyPackageName;
spl_autoload_register(function($cls) {
$cls = ltrim($cls, '\\');
if (strpos($cls, __NAMESPACE__) !== 0) {
return;
}
$classWithoutBaseNamespace = str_replace(__NAMESPACE__, '', $cls);
// Load files from 'src' directory based on their class name without
// the StoutLogic\AcfBuilder namespace.
$path = dirname(__FILE__).
DIRECTORY_SEPARATOR.
'src'.
str_replace('\\', DIRECTORY_SEPARATOR, $classWithoutBaseNamespace).
'.php';
require_once($path);
});
I can't find it in the Composer Documentation, how to set it up in a new project/package. However I can find a bazillions guides on how to use autoload.
As yivi and Daniel Protopopov pointed out:
Check the documentation at getcomposer.org regarding autoloading
Delete your custom autoloader definition, register your namespace in composer.json (hoping you follow PSR-4 already), run composer dump-autoload.
Last but not least, when- and wherever you need to use it, just include the
require __DIR__ . '/vendor/autoload.php';
I'm developing a certain PHP framework for WordPress, and I want to give my users the option to use composer to install it as a package, or to install it manually. If composer is used, then a psr-4 class autoloader handles everything. If not, then all files must be loaded manually during the framework's bootstrapping process.
Is there a safe way to check whether composer's autoloader is being used in a given WordPress environment?
Currently i'm using:
if( !class_exists('Composer\\Autoload\\ClassLoader') )
{
// Manually include files if composer is not used.
require_once 'some/files.php';
}
However, if in a given WordPress environment there is a plugin that uses composer internally, then the above if statement will return true even though other plugins have no access to it.
The solution, as it turns out, is quite simple. You need to create 2 different bootstrapping files, say manual-bootstrap.php and composer-bootstrap.php. Then add the following lines to composer.json:
"autoload": {
"files": ["composer-bootstrap.php"]
}
composer-bootstrap.php will only be called if composer is used to load the framework as a dependency. Users that want to load the framework manually will use manual-bootstrap.php instead.
if(class_exists("\Composer\Autoload\ClassLoader"))
{
// a composer autoload.php has already been included/required
}
I am using Codeigniter since a year and I also started learning Laravel recently and I noticed that having a composer in your framework really helps you in a many ways.
I notice that the Codeigniter 3 has this option in config.php file to add a composer in it.
$config['composer_autoload'] = TRUE;
so I am thinking to add a composer in my CI.
Is it best practice to add a Composer in CI?
What should be the directory structure for that and is it work smoothly with the CI?
This is how I implemented composer in CodeIgniter 3.It is very easy. You have to install composer on your machine and I think you have it because you use laravel.
First copy and paste composer.json file in the project folder to application folder
Secound in the config.php file $config['composer_autoload'] = TRUE;
Now you have composer in your project. I will saw you an example like how to install mpdf using composer
Open cmd and direct to application folder
Inside application directory Type composer require mpdf/mpdf
Now a vendor folder will be created inside application folder and inside vendor
folder you can see all your packages downloaded by composer.
Now since you autoloaded composer now you can just use the code given by mpdf official manual like
function m_pdf()
{
$mpdf = new mPDF();
// Write some HTML code:
$mpdf->WriteHTML('Hello World');
// Output a PDF file directly to the browser
$mpdf->Output();
}
You can install any packages in https://packagist.org/ like mpdf very simply like this. Remember you don't need to type require_once APPPATH.'/vendor/mpdf/mpdf/mpdf.php'; since you already autoloader composer. If not prefer to autoload composer you must type require_once APPPATH.'/vendor/mpdf/mpdf/mpdf.php' at the beginning of each controllers where you use the mpdf vendor libraries.
in my experience, the best way is installing composer and dependencies independent of CodeIgniter, like 'composer require ', and then give access to the packages in your base controller.
add a file MY_Controller.php to application/core folder and make it extend CI_Controller. then add require_once(APPPATH . 'vendor/autoload.php') at the top. make all your controllers extend this base class, and you have access to all your packages. the same goes if you wanna access them in your models. make the base class MY_Model.php
I'm new to Composer, namespaces, and autoload and I wasn't able to figure out where to write my code (under vendor?).
I have created a directory named ilhan under the vendor, and a file named People.php. Then in the main index.php file using use ilhan\People.php as People; doesn't work because I think it must have been written in autoload_namespaces.php initially.
But if I register ilhan as a vendor then I think Composer will look into the packagist.org which it isn't there.
Create ilhan inside root of your project directory, not in vendor directory and put following in your composer.json,
"autoload": {
"psr-4": {
"Ilhan\\": "ilhan/"
}
},
Most probably you already have psr-4 autoload config added in your composer.json file if you are using some sort of framework, in that case just add "Ilhan\\": "ilhan/" in to it.
Now create People.php inside ilhan directory with following content
<?php
namespace Ilhan;
class People{}
Make sure require __DIR__.'/vendor/autoload.php'; is included in index.php any how, then run composer dump-autoload.
Now in index.php just bellow require __DIR__.'/vendor/autoload.php'; following should work,
use Ilhan\People;
But why do you want to use People class in index.php?
Your code goes into the root directory of your project (or any subdirectory). The vendor folder is only for packages/libraries downloaded by composer, you should never change anything in there.
To start a project just create a new file e.g. /my-project/index.php and require the autoload.php which is automatically created by composer:
<?php
require __DIR__.'/vendor/autoload.php';
// here comes your project code
For more information about autoloading see the official composer documentation at Basic Usage: Autoloading
I am trying to include the YouTube Analytics Service of Google but I can not access it through the Vendor folder.
include(app_path.'path/to/analytics/Google_YoutubeAnalyticsService.php')
It is not working, because it defaults to the App folder.
How can I get out of the App folder and into the Vendor folder (where the YouTube Analytics file is at)?
The error is {
include(C:\xampp\htdocs\mysite\app/path/to/analytics/Google_YoutubeAnalyticsService.php):
failed to open stream: No such file or directory
From where do you want to include that file ?
Place a reference to your file in composer.json autoload object:
"autoload": {
"files":["your_file_path"]
}
Run composer dumpautoload, and you'll have your file :)
Actually you have in the helpers function the path so basically the function base_path give the direction to the root of your project so
echo base_path() . '/vendor';
Should be the route to your vendor folder.
You can se all the documentation in
Helper Functions Laravel
Be sure that you are seeing the documentation of the laravel version that you are using (I put the link for the 4.2 version).
This question was asked a long time ago and the answers reflect that. Most the time now all you need to do is import it using the "use" statement if you installed it with composer. Composer will already reference all the important directories.
It should be something like this, but it will vary depending on the project.
use FolderNameUsuallyGitHubUserName\ClassNameorGitHubProjectName\Class;
That could include a base class as well as some exception classes.
use FolderNameUsuallyGitHubUserName\ClassNameorGitHubProjectName\ClassException;
Usually most packages if compliant with modern composer and php standards work in this fashion.