I'm using cakephp and phpwhois
I'm copying phpwhois folder in app/vendor
in my controller :
App::import('Vendor', 'phpWhois\Whois', array('file' => 'phpWhois/Whois.php'));
whois class is extended with whpisclient class
and show me this error :
Error: Class 'phpWhois\WhoisClient' not found
File: C:\xampp\htdocs\padweb\app\Vendor\phpWhois\Whois.php
If I use composer autoloader, it shows me the same error.
see this nice article http://ceeram.github.io/blog/2013/02/22/using-composer-with-cakephp-2-dot-x/
about using composer in cakephp 2.0
it will allow you to use a global autoload.php file for all your new packages that you install with composer
let me know if it helps
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 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
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.
I try to create my own composer library. I've chose to use psr4 for autoloading mechanism. It works fine with the library project but something goes wrong when I add this library to another project as dependency. I expect the library project create an instance of a class which is located in main project. However this class cannot be found by composer autoloader.
My library project source is here : https://github.com/brnogz/kwinsey
My example project which uses this library like that(HelloWorld class is located in controller/HelloWorld.php file) : https://gist.github.com/brnogz/e27a1dd40ba00b818b23fe7ab8815fad
Please move all your sources to a src subfolder and use "src/" as the PSR-4 target folder. Autoloading from a project root folder is pretty much undefined behaviour.
I am trying to develop a Laravel package with a helper function, which returns a view. I have uploaded on GitHub already https://github.com/faisalahsan/laravel-breadcrums. When I install it through Packagist https://packagist.org/packages/faisalahsanse/laravel-breadcrums, it installs successfully, but when I register it in the provider array in my app.php as Faisalahsanse\Breadcrums\BreadcumsServiceProvider::class,. It gives the following error:
Class 'Faisalahsanse\Breadcrums\BreadcumsServiceProvider' not found
I don't know where I am getting wrong.
Any suggestions?
Your namespace is wrong https://github.com/faisalahsan/laravel-breadcrums/blob/master/src/BreadcumsServiceProvider.php#L2
It should be Faisalahsan\LaravelBreadcrums. As this namespace you are adding in the composer.json file in psr-4 autoload.
Also your provider to add will be Faisalahsan\Breadcrums\BreadcumsServiceProvider::class