Laravel 5: add custom library in sub folder of "Libraries" folder - php

I need to add a custom library to Laravel 5, but I want to add in a sub folder of a "Libraries" folder.
I mean, I have "Libraries" folder inside "app" folder, and I would like to add another folder inside "Libraries" folder and put a class inside it.
What I've done is:
Created "Libraries" folder inside "app" folder;
Created "FusionChartsWrapper" folder inside "Libraries" folder;
Created "FusionCharts.php" file inside "FusionChartsWrapper" folder.
The FusionCharts class has the correct namespace:
namespace App\Libraries\FusionChartsWrapper;
but I can not use it, cause I get this Laravel error:
Class 'App\Libraries\FusionChartsWrapper\FusionCharts' not found
If I move the class inside the "Libraries" folder, it works.
Any idea?

The best way is to add the whole folder to autoload in composer.json file
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/Libraries" // =>folder you want to add
],
"psr-4": {
"App\\": "app/"
},
},
then run composer dump-autoload
Now every files in the Libraries folder can be access every where
and no need to use
namespace App\Libraries\FusionChartsWrapper;
just add \ before function you want to use directly

Related

How to include class in symfony 5?

I have a php file with Paysafecard class. File is called Paysafecard.php. I'd like to include it in my controller. How can I do it?
If you are using Symfony surely you are using the composer autoload.
Go to the root directory path of your project and search the composer.json file.
Within it you find a directive like this :
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
This sentence indicates the mapping between the name and the directory.
Now look at the actual file path, for example /src/lib/Paymenterio.php
Go to the controller where you want to use the class replace the path with the autoload alias and remove the extension.
use App\lib\Paymenterio;

Linking a new Helper File in CakePhp 3 via Composer

I am using Cake PHP 3, and under the src folder, I created a new folder called Helper and created a new file inside it. And now I went to composer.json and added the required file key inside the autoload. You can see it below:
"autoload": {
"psr-4": {
"App\\": "src"
},
"files": [
"./src/Helper/general_helper.php"
]
}
But after, doing composer dump-autoload, getting require(/opt/lampp/htdocs/project_name/vendor/composer/../../src/Helper/general_helper.php):
Its not under the vendor folder, I think that's why its giving the error. Any idea, how to solve this issue, cause I want to maintain a separate folder. Thanks.

Autoload folder without changing composer.json file - Laravel 5.4

I've created a package that creates a folder in the root of the project.
Creating it in the app folder is for me not clean enough. Cause I don't want it to look like it's merged with the laravel framework.
This package is for our company and will be used a lot.
So instead of changing the composer.json file everytime to add the folder to the autoloader I'm trying to just autoload it from the package.
Is something like that possible and how?
Are you saying that you do not want to add it in your composer.json file here?
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Company\\": "company/"
}
},
That is what I would do.
What if you just use the folder structure as the autoloader namespace? That should work. For example:
<?php
use Company\Foo;
new Bar();
Where you would have a folder called company/Foo with all the classes inside declaring their namespace like so:
<?php
namespace Company\Foo;
class Bar {
//
}

Autoloading multiple PHP projects via composer

Assume I have folder layout as
projectA
projectA\src
projectA\vendor\autoload.php
projectB
projectB\src
projectB\vendor\autoload.php
projectC
projectC\src
projectC\vendor\autoload.php
These projects need to be in the same level and they need to coexist with each others, e.g. projectA might use codes from projectB and projectC and vice vera, so they cannot be placed into the vendor folder.
The thing is: the autoload.php in each project is able to autoload their own src and vendor folder, but how to autoload for the others as well?
Assume their neighbor's project will have the folder name as the PHP namespace, is it possible to setup a autoload.php (via composer) such that in the future when I add new project folder, the autoload will magically work?
You can write in each project a composer.json with custom autoload configuration..
examples:
ProjectA:
"autoload": {
"psr-0": {
"ProjectB\\": "path/ProjectB/src/",
"ProjectC\\": "path/ProjectC/src/"
}
},
ProjectB:
"autoload": {
"psr-0": {
"ProjectA\\": "path/ProjectA/src/",
"ProjectC\\": "path/ProjectC/src/"
}
},
Composer is conceived for manage dependencies of single project.. Load more autoload.php of different projects is not a good idea..
but with this method you can create a complete autoloader for each projects

PHP Namespace directory structure

It is my understanding that namespaces are declared as follows : vendor\module\classname.
And directory structure would be:
/Acme
/Module1
/src
/Module1
/Class.php
And a valid way to instantiate the class is : new Acme\Module1\Class();
Are my assumptions correct? If so, what's the benefit of using the src folder?
Convention usually.
If you're using composer and you want to autoload your classes, it looks like you're trying to do PSR-0 autoloading.
So if you have "Acme": "src/"
Then in the same directory as your composer.json file would be a src folder, and in the src folder would be a Acme folder. All files in that folder would be namespace Acme; and all files in subdirectories of that folder would be namespace Acme\Subdirectory;.
In the console you need to type composer dump-autoload every time you make changes to the autoloading part of your composer.json file. (or for classmap autoloading, every time you add new files to directories being autoloaded.) This causes the files generated by composer to be updated. (you can find these files in the vendor/composer directory).
So if there was a src/Acme/Subdirectory/ClassName.php with a class in it named ClassName then to instantiate that class you could type new \Acme\Subdirectory\ClassName();
edit: so if this is your composer.json file
{
"autoload": {
"psr-0": {
"Acme": "src/"
}
}
}
then your directory structure would be something like this
/ProjectRoot
composer.json
/src
/Acme
/Module1
Class.php
And you would create a new instance of your class by typing
new \Acme\Module1\Class();

Categories