You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 7.29.3中无法成功覆盖Omnipay-AuthorizeNetRecurring扩展的Schedule.php文件问题求助

Laravel 7: Fixing Failed Vendor Class Override (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.php into 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/Overrides directory first for Omnipay\AuthorizeNetRecurring classes, 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.php for the Omnipay\AuthorizeNetRecurring\Objects\Schedule entry—it should point to your app/Overrides file path. If not, re-run composer dump-autoload -o.
  • Clear Laravel’s cache to eliminate any lingering references:
php artisan config:clear
php artisan cache:clear

内容的提问来源于stack exchange,提问作者DikoB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 08:44:06