I'm trying to add an external library to symfony.
I've tried this on the app/autoload.php:
$loader->add('LibCokeId',__DIR__ . '/../vendor/libcokeid/libcokeid/lib');
However when I try to use it in a controller:
use libCokeId\LibCokeId
Libcokeid::init()
I get the miss use statement error.
Any help?
In the situation where you have a library that doesn't use composer and you can't retrieve it from packagist, you can manipulate the Composer autoload.
Simply add the class in the composer.json files, as example:
"autoload": {
"psr-0": { "": "src/" },
"files": [
"vendor/folder/my_custom_lib/myFiles.php",
"vendor/libcokeid/libcokeid/lib/libCokeId/LibCokeId.php"
]
},
OR you can Autoload the whole folder in composer.json:
"autoload": {
"psr-0": { "": "src/" },
"classmap": [
"vendor/libcokeid/libcokeid/lib"
],
},
Remember to make a composer install after setting this.
Hope this help.
Related
I am trying to override a package file, as it doesn't work without modification and it hasn't been updated to fix this.
Package is webklex\laravel-pdfmerger and the file is PDFMerger.php
In it has:
use fpdi\FPDI;
for it to work correctly it needs to be:
use FPDI;
Obviously whenever I update using composer, this file is of course then incorrect, and at this stage I have to manually go in and change the file to get it working.
There must be a better way?
I've attempted making a copy of the git package, and then using composer to import it and attempt to override, composer snippet is below, but it doesn't do what I want it to do.
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"repositories": {
"laravel-pdfmerger": {
"type": "package",
"package": {
"name": "****/laravel-pdfmerger-mirror",
"version": "1.1.1",
"source": {
"url": "https://bitbucket.org/****/laravel-pdfmerger-mirror.git",
"type": "git",
"reference": "origin/master"
}
}
}
},
"require": {
...
"****/laravel-pdfmerger-mirror": "1.1.1",
"webklex/laravel-pdfmerger": "1.1"
},
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"VendorExtensions\\xerolaravel\\": "packages/vendorExtensions/xerolaravel",
"Webklex\\PDFMerger\\PDFMerger\\": "vendor/****/laravel-pdfmerger-mirror/src/PDFMerger"
}
},
Any help here would be great.
--- EDIT ---
updated my composer.json file to remove the repository and change the psr-4 autoload to the following:
psr-4": {
"App\\": "app/",
"VendorExtensions\\xerolaravel\\": "packages/vendorExtensions/xerolaravel",
"Webklex\\laravel-pdfmerger\\PDFMerger\\": "vendor/webklex/laravel-pdfmerger/src/PDFMerger"
}
But it is still not overriding the original file whenever I do use PDFMerger. Is it because I'm using a Facade?
Figured this one out at last...
1) Import Forked respository in your composer.json file, but ensure the name matches the name of the package you are replacing; in my case webklex/laravel-pdfmerger, like so:
"repositories": {
"webklex/laravel-pdfmerger": {
"type": "package",
"package": {
"name": "webklex/laravel-pdfmerger",
"version": "1.1.1",
"source": {
"url": "url to git",
"type": "git",
"reference": "origin/master"
}
}
}
},
Note: Ensure you set a version higher than what is currently available on the original package, so here I added .1 to differentiate the change and tag the commit as this so I can select it.
2) require the package, but ensure you request the version you tagged in your commit:
"require": {
"php": ">=5.6.4",
"webklex/laravel-pdfmerger": "1.1.1"
},
3) Update the autoload to point to where the package has been imported:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Webklex\\PDFMerger\\": "vendor/webklex/laravel-pdfmerger/src"
}
},
Run composer update and then dump the autoload.
You should then be able to reference the package in your application as per the instructions in the original package.
You can do this by putting your edited package in a vendor-custom directory and adding it to the autoload section of your composer.json.
"autoload": {
"psr-4": {
"App\\": "app/",
"Vendor\\Package": "vendor-custom/vendor/package"
}
},
Gregwar psr-0 example:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"psr-0": {
"Gregwar\\Cache": "vendor-custom/gregwar/cache",
"Gregwar\\Image": "vendor-custom/gregwar/image",
"Gregwar\\ImageBundle": "vendor-custom/gregwar/image-bundle"
}
},
In PHP, you can actually alias a class to another, and that behaviour include namespaces.
This is extremely useful in legacy projects where you want to start using namespaces but don't want to refactor all usage of the said class.
You have the opposite here, the class is defined in a namespace but the class you are using is not in a namespace, still, aliasing the class would do.
So the function you might be looking for is class_alias.
Here is an example
<?php
class FPDI {
public function __construct() {
var_dump('I am FPDI');
}
}
class_alias('FPDI', 'fpdi\FPDI');
$noNamespace = new FPDI; // would, off course work
$namespace = new fpdi\FPDI(); // would work too, thanks to the class alias
So if you are using Laravel, your job should be as simple as aliasing the class correctly.
This can actually be done where you define your providers and aliases.
So at the beginning of config/app.php
<?php
class_alias('FPDI', 'fpdi\FPDI');
return [
// all of the below array stays as you have it, it was just snipped for concision purpose
];
i try to create a bundle by console with symfony 3.3.8 after this version i dont have any problem with that but now i have one,
image1
image2
Any suggestion please!!!!!!
In your composer.json
replace your autoload part with:
"autoload": {
"psr-4": {
"": "src/"
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
thinks i also execut the command bellow
composer dump-autoload.
Problem resolve.
I have an RSA algorithm Library giving to me by a payment gateway and When I do a
include (app_path().'/PaymentGateway/Crypt/RSA.php');
this and try to make an object as $rsa = new Crypt_RSA(); this it gives me and error saying
Class 'App\Http\Controllers\Crypt_RSA' not found
I tried including it in web.php and making an object it worked the problem occur when I try to include it in a Controller.
This is what I did. Oh and a little back ground I use to have this in Laravel 4, PHP 5, jpgraph 2.
I am using jpgraph 4.1 on Laravel 5.5 using PHP 7.
Created a folder under app called jpgraph
Placed the src folder that is in the tarball of jpgraph in that folder
Created file call Graph1.php, is my code using jpgraph, with the class Custom_GraphsJM in the jpgraph folder.
In composer.json added "app/jpgraph/Graph1.php" to the "classmap"
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/jpgraph/Graph1.php"
],
"psr-4": {
"App\\": "app/"
}
},
In the application folder:
composer dump-autoload
Checked the autoload_classmap.php and I have
'Custom_GraphsJM' => $baseDir . '/app/jpgraph/Graph1.php',
In my Model at the top I have
use Custom_GraphsJM;
To create a class
$Two_Graphs_Temp = new Custom_GraphsJM();
You can tell Composer to autoload any (non-PSR) class by adding the base folder to:
"autoload": {
"classmap": [
"app/commands",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
....
And you can also autoload autoloaders by adding them to the files section:
"autoload": {
"files": [
"temboo/src/Temboo_Loader.php"
],
...
After adding those entries, execute:
composer dumpautoload
And check the file vendor/composer/autoload_classmap.php, the available classes must be all listed in it, if one file is not there it will not be autoloaded.
On default, everything included in the app folder of your laravel project is autoloaded, that is described in the composer.json of your project:
...
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
...
The only thing you will need to do is simply use the namespace:
use App/Path/To/Third/Party/plugin/Class;
If, however, the plugin is placed outside of the scope of App, then simply add it to the psr-4 autoloader:
"psr-4": {
"ProjectRootNs\\": "projects/myproject/"
}
I've made a little library for myself and I'm trying to autoload it into my laravel project, it installs fine but whenever I try to use the class it simply says it's not found.
I've checked all the classmap files in vendor/composer and it doesn't seem to be in any of them.
This is my composer.json for my lib:
{
"name": "my-user/aspect-parser",
"version": "1.0.0",
"type": "package",
"require": {
"nesbot/carbon": "^1.22"
},
"autoload": {
"psr-4": {
"AspectParser\\": "src/"
}
}
}
My file structure is:
AspectParser
src
Parser.php
It was an issue with the type in composer.json. I've changed it to library and it adds to the autoload classmap.
I'm looking at examples, and I cannot get my code to work.
Directory Structure
app
src
company
FileExport
FileExport.php
FileExportInterface.php
Validator
vendor
...
My composer.json
"require": {
"monolog/monolog": "1.9.1",
"ilya/belt": "2.1.1"
},
"autoload": {
"psr-4": {"Company\\": "src"}
}
Namespace is Company\FileExport.
Classes in vendor work fine, but not mine. I've run composer update as well.
Your autoload should look like so
"autoload": {
"psr-4": {"Company\\": "src/company/"}
}