Composer with Koseven (Kohana) - php

I'm developing an application where I started using Kohana and now Koseven, and I need to use an api that was available in the composer, I followed the steps to download the files I created a folder to sell inside the application, and put in the bootstrap.php code to call the autoload of the composer.
But after doing this when trying to use a class of this api error of "class not found" occurs.
I do not know what else to do, can you help me?

In order to use composer with Kohana or Koseven, you need to call composer's autoloader from within bootstrap.php.
After Kohana::modules($modules); and before Route::set, insert the following code:
/**
* Autoload composer libraries
*
*/
require APPPATH . 'vendor/autoload.php';
This assumes your composer install command is run the from the root of your app, and it uses the default vendor directory.

Probably you must add this to your composer.json. (Check inc composer doc.) I don't know, because in modules directory I have second instance.
"extra": {
"installer-paths": {
"modules/{$name}/": ["type:kohana-module"]
}
},
And enable module composer as first:
Kohana::modules(array(
'composer' => MODPATH.'composer', //
'auth' => MODPATH.'auth', // Basic authentication
'cache' => MODPATH.'cache', // Caching with multiple backends
It works for me ko3

Related

Having real problems with Composer

I have a dedicated server with WHM and cPanel. It already has composer installed on it.
I'm trying to run the following php lines in a webpage:
require __DIR__ . '/../vendor/autoload.php';
// Configure API key authorization: JWT
$config = \Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Authorization', '[my api key]');
$apiInstance = new \Swagger\Client\Api\MessagesApi(
new \GuzzleHttp\Client(),
$config
);
I've created a composer.json in the public_html folder and put the following in it:
{
"autoload": {
"psr-4": { "Swagger\\Client\\" : "lib/" }
}
}
And then I ran composer update in the terminal which seemed to install the dependencies and all the relevant files.
It's seeing the autoload.php file but I'm still getting a class not found error:
Fatal error: Uncaught Error: Class 'Swagger\Client\Configuration' not found in /home/mywebsite/public_html/converter/sms.php:9 Stack trace: #0 {main} thrown in /home/mywebsite/public_html/converter/sms.php on line 9
I've been at this for 4 hours now. What am I doing wrong? I can't find anything online that will guide me in the right direction.
In Composer Basic Usage documentation about autoloading it says
For libraries that specify autoload information, Composer generates a vendor/autoload.php file. You can include this file and start using the classes that those libraries provide without any extra work
So, as #NicoHaase said in the comments, if you installed Swagger with Composer (adding a require key and running composer update, for example) you don't need to specify the autoload path to Swagger in composer.json.

Composer autoload in Wordpress custom plugin

I'm developing a brand new Wordpress plugin and I would like to use Composer to autoload classes.
Here is the plugin directory heriarchy:
my composer.json content:
{
"autoload": {
"psr-4": {
"G4S_ECommerce\\": "src"
}
}
}
In the directory where composer.json is, on cmd, I execute:
composer install -> this generates the vendor/composer folder and the vendore/autoload.php.
composer composer dumpautoload -o -> outputs "Generated optimized autoload files containing 0 classes"
In the main file G4S_Ecommerce.php I put the following line:
require __DIR__.'/vendor/autoload.php';
In the same file I put
use G4S_Ecommerce\Includes\Ecommerce;
$starter = new Ecommerce();
but it leads me to a Fatal error: Uncaught Error: Class 'G4S_Ecommerce\Includes\Ecommerce' not found
Why the composer dumpautoload -o returns 0 classes? What am I doing wrong?
Thanks
First (it is not obvious from your files structure) you need to set a namespace for your Ecommerce class (i.e., G4S_Ecommerce/Includes)
Second, based on what you've declared in the autoload directive, composer is expecting to find the G4S_Ecommerce folder under the src folder, and in that folder you need to place your php class file with a name identical to the class name (i.e., Ecommerce).

How do I require a file or class in a vendor library in Yii2 that is not already included?

I have this class that Yii2 cannot see. All the other classes work. I tried the following. The commented lines did not work.
// use app\vendor\googleads\googleads-php-lib\src\Google\Api\Ads\Common\Util\ErrorUtils;
// require_once UTIL_PATH . '/ErrorUtils.php';
require_once('../vendor/googleads/googleads-php-lib/src/Google/Api/Ads/Common/Util/ErrorUtils.php');
use \ErrorUtils;
This works, but it doesn't look right. Also it doesn't work in the command mode, which I need.
$ yii cron
PHP Warning: Uncaught exception 'yii\base\ErrorException' with message 'require_once(../vendor/googleads/googleads-php-lib/src/Google/Api/Ads/Common/Util/ErrorUtils.php): failed to open stream: No such file or directory' in /cygdrive/c/Users/Chloe/workspace/xxx/models/GoogleAdWords.php:36
How can I require or use this class in Yii2?
Fisrt addto composer (shell command):
$ composer require googleads/googleads-php-lib
Then simply use te class:
\ErrorUtils::GetApiErrors($var);
Note that googleads don't use namespaces so it's "\" NS
The library that you use doesn't provide psr-4 autoloader settings for its classes. You need to add autoload classmap for classes that you want to load, into your composer.json in project's root directory like following:
"autoload": {
"classmap": [
"vendor/googleads/googleads-php-lib/src/Google/Api/Ads/Common/Lib",
"vendor/googleads/googleads-php-lib/src/Google/Api/Ads/Common/Util"
]
},
and then in your console: composer dump-autoload
This will update the composer autoloader. After that you'll be able to call library classes with: \ErrorUtils::GetSourceOperationIndex($error)

How autoloader works in zend framework 2

I am just trying to create a package for packagist, a zend framework 2 user authentication module, https://packagist.org/packages/tahmina8765/zf2auth
When I keep this zf2auth folder in my modules folder, it works. But when I download it with composer, it downloaded in vendor/tahmina8765/zf2auth.
In this folder, it does not work. I mean, I have added this module in application.config.php -
'modules' => array(
...
'Zf2auth'
),
but here it does not work. If I keep it on step ahead, ie. vendor/zf2auth it works again.
How can I make it workable in vendor/tahmina8765/zf2auth folder?
I think there's a problem with the composer.json file in your module. It says:
"autoload": {
"psr-0": {
"Zf2auth": "./"
}
}
and looking at the directory structure it should be:
"autoload": {
"psr-0": {
"Zf2auth\\": "src"
}
}
It works in your ./modules/ dir because of the getAutoloaderConfig you have in your module class -- that's done by ZF2. When you download the module through composer however, the autoloading (probably) get's done by composer and the autoload (invalid) definition in your composer manifest is used. You might also want to run composers dump command afterwords, to refresh the autoloading classes.

Do not use Illuminate in Laravel packages, Laravel manual modules

When creating a package in Laravel, the packages use "\Illuminate\Support\ServiceProvider" in their *ServiceProvider.php. Which is located in the package \vendor directory.
public function boot()
{
$this->package('faiawuks/articles');
include __DIR__.'/../../../routes.php';
}
As you see I need Illuminate: $this->package to register my package specific routes.
I've noticed that Illuminate also exists inside the main vendor directory. Is it possible to remove the Illuminate vendor package for my created workbench package and use the main vendor\Illuminate package? I'ts going to be a private workbench package anyway?
I want to create 5+ packages for my application, so I can split it into 'modules'.
Solved it for now by creating my own directory structure in the directory ./modules and using this in composer.json:
"psr-0": {
"Articles": "modules/"
}
inside the "autoload": { part. So my structure is as following:
mylaravelproject
- modules
- Articles
- *othermodules
The PSR-0 autoloader looks for classes inside ./modules/Articles. The namespace to use in the Articles directory is:
namespace Articles;

Categories