Laravel 7.29.3中无法成功覆盖Omnipay-AuthorizeNetRecurring扩展的Schedule.php文件问题求助
I've run into this exact issue before with overriding vendor classes in Laravel, so let's break down why your current approach isn't working and fix it step by step.
Why Your Current Setup Fails
The core problem is that your custom Schedule class uses the Cimpleo namespace, while the original vendor class lives under Omnipay\AuthorizeNetRecurring\Objects. When your controller references use Omnipay\AuthorizeNetRecurring\Objects\Schedule;, it’s still pulling the vendor class—your custom version isn’t even in consideration because it’s in a completely separate namespace.
Step-by-Step Fix
1. Match the Original Namespace & File Structure
First, replicate the vendor class’s directory structure in your app/Overrides folder to align namespaces:
- Create this path:
app/Overrides/Omnipay/AuthorizeNetRecurring/Objects/ - Copy the original
Schedule.phpinto this new directory - Update the namespace in your custom file to exactly match the vendor class:
namespace Omnipay\AuthorizeNetRecurring\Objects; // Keep this identical to the original use Academe\AuthorizeNet\PaymentInterface; use Academe\AuthorizeNet\AbstractModel; use Omnipay\Common\Exception\InvalidRequestException; use DateTime; class Schedule extends AbstractModel { // Your custom modifications here—for example, fix the interval length restriction: public function setIntervalLength($value) { // Remove the 7-365 limit since you need to use 1 for monthly intervals if (!is_numeric($value)) { throw new InvalidRequestException('Interval Length must be a numeric value.'); } $this->setParameter('intervalLength', (string) $value); return $this; } // Rest of the class code... }
2. Update composer.json Autoload Configuration
Modify your autoload section to prioritize your custom directory for the Omnipay\AuthorizeNetRecurring namespace, and ensure the original vendor file is excluded:
"autoload": { "psr-4": { "App\\": "app/", "Omnipay\\AuthorizeNetRecurring\\": "app/Overrides/Omnipay/AuthorizeNetRecurring/" }, "classmap": [ "database/seeds", "database/factories", "vendor/google/apiclient/src", "vendor/google/apiclient-services/src/Google" ], "exclude-from-classmap": [ "vendor/cimpleo/omnipay-authorizenetrecurring/src/Objects/Schedule.php" ] }
- The PSR-4 mapping tells Composer to check your
app/Overridesdirectory first forOmnipay\AuthorizeNetRecurringclasses, before falling back to the vendor folder. - Using forward slashes avoids Windows path escaping issues.
3. Refresh Composer’s Autoloader
Run the optimized autoload command to apply changes and clear cached mappings:
composer dump-autoload -o
The -o flag generates an optimized class map, ensuring your custom class takes priority.
4. Verify Controller Code
You don’t need to change your controller’s use statement—since the namespace matches, it will now load your custom Schedule class automatically:
// This now references your app/Overrides version, not the vendor one use Omnipay\AuthorizeNetRecurring\Objects\Schedule;
Troubleshooting Tips
- If the vendor class still loads, check
vendor/composer/autoload_classmap.phpfor theOmnipay\AuthorizeNetRecurring\Objects\Scheduleentry—it should point to yourapp/Overridesfile path. If not, re-runcomposer dump-autoload -o. - Clear Laravel’s cache to eliminate any lingering references:
php artisan config:clear php artisan cache:clear
内容的提问来源于stack exchange,提问作者DikoB

