{"record":{"id":"2bda13cd32af65fd","repo":"phalcon/cphalcon","slug":"routerfactory-load-requires-an-array-or-phalcon-c","errorCode":null,"errorMessage":"RouterFactory::load requires an array or Phalcon\\Config\\ConfigInterface instance","messagePattern":"RouterFactory::load requires an array or Phalcon\\\\Config\\\\ConfigInterface instance","errorType":"exception","errorClass":"Phalcon\\Mvc\\Router\\Exceptions\\InvalidRouterFactoryConfig","httpStatus":null,"severity":"error","filePath":"phalcon/Mvc/Router/RouterFactory.zep","lineNumber":52,"sourceCode":" * );\n *```\n */\nclass RouterFactory\n{\n    /**\n     * Builds a Router from a config array or ConfigInterface and loads routes.\n     *\n     * @param array|\\Phalcon\\Config\\ConfigInterface config\n     *\n     * @return RouterInterface\n     */\n    public function load(var config) -> <RouterInterface>\n    {\n        var defaultRoutes, router;\n\n        if typeof config === \"object\" {\n            if !(config instanceof ConfigInterface) {\n                throw new InvalidRouterFactoryConfig();\n            }\n            let config = config->toArray();\n        }\n\n        if typeof config !== \"array\" {\n            throw new InvalidRouterFactoryConfig();\n        }\n\n        let defaultRoutes = true;\n        if isset config[\"defaultRoutes\"] {\n            let defaultRoutes = (bool) config[\"defaultRoutes\"];\n        }\n\n        let router = this->newInstance(defaultRoutes);\n        router->loadFromConfig(config);\n\n        return router;\n    }","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Mvc/Router/RouterFactory.zep#L34-L70","documentation":"RouterFactory::load() (and RouterFactory::newInstance(), which shares the same config path in phalcon/Mvc/Router/RouterFactory.zep) accepts either a plain PHP array or an object implementing Phalcon\\Config\\ConfigInterface. This throw fires at line 52 when you passed an object that is NOT a ConfigInterface instance, so the factory cannot call toArray() on it to normalize the config.","triggerScenarios":"Calling (new \\Phalcon\\Mvc\\Router\\RouterFactory())->load($obj) where $obj is \\stdClass (e.g. from json_decode()), ArrayObject, or a custom config class that does not implement Phalcon\\Config\\ConfigInterface.","commonSituations":"Migrating Phalcon 4 code to 5 where \\Phalcon\\Config moved to \\Phalcon\\Config\\Config; passing json_decode() output without a true second argument; passing a domain object or collection instead of config; wrapping route config in a custom wrapper class.","solutions":["Pass a plain array of route definitions instead of an object","If you have a Config object, ensure it is \\Phalcon\\Config\\Config (implements ConfigInterface) or call ->toArray() before load()","For custom config classes, implement Phalcon\\Config\\ConfigInterface (toArray, path, merge, etc.)"],"exampleFix":"// before\n$factory = new \\Phalcon\\Mvc\\Router\\RouterFactory();\n$router = $factory->load(json_decode($json)); // stdClass -> InvalidRouterFactoryConfig\n\n// after\n$factory = new \\Phalcon\\Mvc\\Router\\RouterFactory();\n$router = $factory->load(json_decode($json, true)); // plain array","handlingStrategy":"type-guard","validationCode":"if (!(is_array($config) || $config instanceof \\Phalcon\\Config\\ConfigInterface)) {\n    throw new \\InvalidArgumentException('RouterFactory config must be array or ConfigInterface');\n}","typeGuard":"function acceptsRouterConfig(mixed $config): bool\n{\n    return is_array($config) || $config instanceof \\Phalcon\\Config\\ConfigInterface;\n}","tryCatchPattern":"try {\n    $router = $factory->load($config);\n} catch (\\Phalcon\\Mvc\\Router\\Exceptions\\InvalidRouterFactoryConfig $e) {\n    // normalize then retry once with the array form\n    $router = $factory->load(is_object($config) && method_exists($config, 'toArray') ? $config->toArray() : []);\n}","preventionTips":["Always build route definitions as plain arrays","If wrapping config, implement ConfigInterface or expose toArray()","json_decode(..., true) for arrays, never stdClass"],"tags":["phalcon","router","config","type-validation","di"],"backgroundTag":"config-type-mismatch","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}