I am having trouble with a Laravel application I am developing using Vagrant.
I have created a few custom classes to help me query an external API, but keep receiving a ReflectionException with the message 'Class ExtAPI does not exist'. These classes are under "app\Lib\ExtAPI"
I am using the Service Container to get the class.
Here is the composer.json:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"App\\Models\\":"app/Models/*",
"App\\Interfaces\\":"app/Interfaces/*",
"App\\Repositories\\":"app/Repositories/*",
"App\\Lib\\":"app/Lib/*"
}
},
I don't understand what I am doing wrong because this setup was working previously. Since running my setup on a different host, this is not working anymore.
Any help would be much appreciated.
Related
I made my first php library available through packagist. I'm still figuring out how it works, but I keep getting this exception:
Fatal error: Uncaught Error: Class 'Waspeer\NextAction\FabianBeiner\Todoist\TodoistClient' not found in /home/deb58323n2/domains/wannessalome.nl/public_html/assets/todoist/vendor/waspeer/todoist-next-action/src/NextAction.php:120
I listed FabianBeiner\Todoist as a dependency in my composer.json:
"require": {
"php": ">=7.1.0",
"fabian-beiner/todoist-php-api-library": "^0.7.2"
},
"autoload": {
"psr-4": {
"Waspeer\\NextAction\\": "src/"
}
}
And this is in my library file:
namespace Waspeer\NextAction;
use FabianBeiner\Todoist\TodoistClient;
It seems like it's trying to find the dependency in the folder of my library, but I don't know why and how to solve this. Any ideas?
Solved it! When I changed new FabianBein\Todoist\TodoistClient later in the code to just new TodoistClient it worked. Guess I don't really get yet how this use statement/autoloading works..
I'm trying to add this SDK in my Laravel 5.4 application. I downloaded and installed it in app/resources/assets and guessing that I can use it in controllers as:
require_once '[ruta/payu-php-sdk]/lib/PayU.php';
The SDK have some Eviromental variables and Keys.
What do you think is the better approach for this type of SDK in laravel?
I'd suggest using Omnipay, it's a well supported library the has integrations for many different gateways, including PayU. This library is framework agnostic, so you won't need to change your code should you need to change payment gateways.
You can use composer to add these libraries:
"require": {
"omnipay/omnipay": "~2.0",
"omnipay/payu": "~2.0"
}
This way you don't have to keep track of requires.
Edit: To add any type of library that doesn't use composer already, You could manually tell composer to autoload file(s).
For your case, there should be a section in your composer.json called autoload, here's what you could add:
...
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"ruta/payu-php-sdk/lib/PayU.php"
]
},
...
This should make the SDK available throughout the application.
I am trying to include guzzle http client from a vendor folder and using composer. Here is what I have tried so far.
Location of guzzle http client file vendor/guzzle/guzzle/src/Guzzle/Http/Client.php
In composer.json file I included
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"files":["vendor/guzzle/guzzle/src/Guzzle/Http/Client.php"],
"psr-4": {
"App\\": "app/"
}
},
The I ran the command composer dumpautoload.
In my controller I am trying to call an api end point like this
use GuzzleHttp\Client;
$client = new Client(); // this line gives error
$res = $client->get('https://api.fixer.io/latest?symbols=CZK,EURO');
The error is Class 'GuzzleHttp\Client' not found
What I am missing here, please help me. Thanks.
For a better file structure here is a screenshot of of the file location
Short Version: You're trying to instantiate a class that doesn't exist. Instantiate the right class and you'll be all set.
Long Version: You shouldn't need to do anything fancy with your composer.json to get Guzzle working. Guzzle adheres to a the PSR standard for autoloading, which means so long as Guzzle's pulled in via composer, you can instantiate Guzzle classes without worrying about autoloading.
Based on the file path you mentioned, it sounds like you're using Guzzle 3. Looking specifically at the class you're trying to include
namespace Guzzle\Http;
/*...*/
class Client extends AbstractHasDispatcher implements ClientInterface
{
/*...*/
}
The guzzle client class in Guzzle 3 is not GuzzleHttp\Client. Its name is Guzzle\Http\Client. So try either
$client = new \Guzzle\Http\Client;
or
use Guzzle\Http\Client;
$client = new Client;
and you should be all set.
Just recently we decided to upgrade from symfony 2.8 too 3.3 with this being noted I felt it was best to start a fresh symfony project and just pull over some parts of the project and change the key needed items that changed over this major version upgrade. I have worked out most of the issues however currently I get this meaning that I may have more issues that I suspect. here is the current one:
C:\xampp\htdocs\Fresh_Api>php bin/console server:run PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to lo ad class "CommandListener" from namespace "Directive\MultiTenant\MasterBundle\EventListener". Did you forget a "use" statement for another namespace? in C:\xampp\htdocs\Fresh_Api\var\cache\dev\a ppDevDebugProjectContainer.php:580 Stack trace:
#0 C:\xampp\htdocs\Fresh_Api\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Contai ner.php(331): appDevDebugProjectContainer->getAppBundle_CommandListenerService()
#1 C:\xampp\htdocs\Fresh_Api\var\cache\dev\appDevDebugProjectContainer.php(965): Symfony\Component\D ependencyInjection\Container->get('app_bundle.comm...')
#2 C:\xampp\htdocs\Fresh_Api\vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\EventDispatcher.php(229): appDevDebugProjectContainer->{closure}()
#3 C:\xampp\htdocs\Fresh_Api\vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\EventDispatcher.php(61):Symfony\Component\EventDispatcher\EventDispatcher->sortListeners('console.command')
#4 C:\x in C:\xampp\htdocs\Fresh_Api\var\cache\dev\appDevDebugProjectContainer.php on line 580
This is of course after I try running php bin/console server:run
I have tried deleting my cache, deleting my autoload file, deleting my vendor file and reinstalling composer. I have tried to look to see if there are any dependencies that are missing. (I could have missed something here)
The file it's talking about is a generated file so something about it is not properly noticing that that class exists.
Here is the CommandListener
namespace Directive\MultiTenant\MasterBundle\EventListener;
use Directive\MultiTenant\MasterBundle\Connection\ConnectionWrapper;
use Directive\MultiTenant\MasterBundle\Entity\Tenant;
use Directive\MultiTenant\MasterBundle\Repository\TenantRepository;
use AppBundle\TenantProviderInterface;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\InputOption;
The appDevDebugProjectContainer is a little large so I'll wait to see if there are any response to see if it's even needed as I'm sure it's something else that is causing it not to generate the proper information needed.
thanks for any input you have.
EDIT: This is a link here noting it's the same issue however this exact issue there is already resolved before all this. Here is my code for that section.
"autoload" : {
"psr-4": {
"AppBundle\\": "src/AppBundle"
},
"classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},
This was already fixed already. here is another link I used to try to solve my issue that of course had no help.
php bin/console symfony commands give a fatal error
and another
Symfony 3.3 Getting ClassNotFoundException
All Credit goes to Cerad the answer is sadly duplicated however I did not notice something that was needed. the autoloader needs to know where it is pulling classes from and has to be setup with the new way the symfony does it now. Here is my example with my additional code to make it work.
"autoload" : {
"psr-4": {
"AppBundle\\": "src/AppBundle",
"BundleName\\": "src/BundleName"
},
"classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},
The error I am getting is
file: "/var/www/html/goalline/swiftmailer333/Swift.php" line: 32
message: "Cannot redeclare class Swift" type:
"Symfony\Component\Debug\Exception\FatalErrorException"
I have a need to remove Swift from Laravel as it conflicts with functions form a legacy application that my Laravel app needs to call.
How can I do this? Whether I should is irrelevant I have to use those functions from the legacy application.
I've tried commenting
'Mail' => 'Illuminate\Support\Facades\Mail',
and 'Illuminate\Mail\MailServiceProvider' but that didn't work.
You'll have to namespace your Swift class:
<?php
namespace YourApp;
class Swift {
}
Then use it this way:
$swift = new YourApp\Swift;
Another possibility is to create a nasty hack to remove it from your Laravel installation, but to do that you'll have to create a repository of your own and use your repository in your composer.json file:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/yourusername/swiftmailer"
}
],
"require": {
"swiftmailer/swiftmailer": "dev-master"
}
}
Your repository can pretty much be a copy of swiftmailer's which you basically remove every file except the composer.json.