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;
Related
I'm developing a Laravel package but I have a problem with composer autoloading.
My package has 2 folders under src folder. One of them is named Laravel and the other one is Telegram. Here is the package structure:
./packages
.../typhoon
...../src
......./Laravel
........./Providers
............LumenServiceProvider.php
............LaravelServiceProvider.php
......./Telegram
..........Api.php
.....composer.json
This package is developed under SaliBhdr/Typhoon namespace.
I have added the packages/typhoon/src directory in Laravel's composer file like so:
"autoload": {
"psr-4": {
"App\\": "app/",
"SaliBhdr\\Typhoon\\" : "packages/typhoon/src/"
}
},
And add the src/ address in package composer.json file like so:
"autoload": {
"psr-4": {
"SaliBhdr\\Typhoon\\": "src/"
}
},
Here is the strange behavior begins. When I execute the php artisan serve command Laravel throws an error that says :
Class 'Salibhdr\Typhoon\Laravel\Providers\LumenServiceProvider' not found
And if I check if the class exists with class_exists('Salibhdr\Typhoon\Laravel\Providers\LumenServiceProvider') function it returns false. But if I check if Salibhdr\Typhoon\Telegram\Api exists it returns true.
I checked the autoload_classmap file and notice that the composer includes all the classes under Telegram subfolder but not Laravel subfolder.
Why composer acts weird like this? why did it include one subfolder and not the other? It is something that I do every day and never seen anything like this.
I desperately need help. Any help would be appreciated
You are trying to initialize Salibhdr\Typhoon\Laravel\Providers\LumenServiceProvider but in your composer it's "SaliBhdr\\Typhoon\\": "src/".
Notice the capital B in your composer. PHP classes are case sensitive so you have to make sure it's either both lowercase or both uppercase.
Also make sure to run composer dumpautoload if you modify composer.json.
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
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 {
//
}
I have a project in laravel and I am trying to move all of my source files to app/src/{module}. I am autoloading the directory in composer.json.
I am having to declare/import all of the global classes from laravel in my files under src/. For example if I want to use Input I have to say use Input;. How can I access these classes as if the file was in the app/controllers directory?
What I added to composer.json:
"autoload": {
"psr-0": {"Illuminate\\Auth": ""}
},
The top of one of my files under src:
<?php namespace src\proposal;
use Input,JsonResponder,Request,JsonValidator,DB;
class ProposalRepo implements IProposal
{
Just because you are using your own namespaces, you will need to either use global classes like \Input::get() or you will need to put use Input; to the top of your file. At least this is how i solve this issue.
I know this question has already been asked, but it seems that autoloading process changed a little bit with composer.
I just want to add a class library to my silex project.
So I made this file:
vendor\lib\picture.php
<?php
namespace MyNamespace;
class Picture
{
function testage()
{
echo 'hihaaa ça marche'; exit;
}
}
in vendor/composer/autoload_namespaces.php, I added this line to the big array:
'MyNamespace' => $vendorDir . '/lib/',
And in the main file I added:
use MyNamespace\Picture as Picture;
and called it like that:
$app->register(new Picture());
which gives me this error:
Fatal error: Class 'MyNamespace\Picture' not found...
I just don't know how to add a class that I can use from any controller, easily, without command line (I don't use composer, I downloaded silex preconfigured), any idea?
If you're using composer you should not change the vendor directory. You should not add files into it, and you should not modify the composer-generated files.
I recommend you put those classes into the src directory. #gunnx shows how you can configure autoloading in composer.json, so that it gets re-generated every time you run composer install.
The file would be in src/MyNamespace/Picture.php. The autoload config in composer.json would be:
{
"autoload": {
"psr-0": { "MyNamespace": "src/" }
}
}
The actual solution is a combination of the two previous answers. But I think it's important to get the details right ;-).
Your Picture class should be in this file: vendor/lib/MyNamespace/Picture.php. Note the full namespace and the casing.
You can add your own code to the autoloader by adding the following to your composer.json
e.g.
{
"autoload": {
"psr-0": {"Acme": "src/"}
}