Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: config() can't load Config file in a module if there is a Config file with the same name #5356

Closed
kenjis opened this issue Nov 19, 2021 · 2 comments

Comments

@kenjis
Copy link
Member

kenjis commented Nov 19, 2021

PHP Version

8.0

CodeIgniter4 Version

develop (0aa542b)

Which operating systems have you tested for this bug?

macOS

Which server did you use?

cli-server (PHP built-in webserver)

Database

No response

What happened?

config() can't load Config file in a module if there is a Config file with the same name.

Steps to Reproduce

Configure 'Acme\Blog namespace:

--- a/app/Config/Autoload.php
+++ b/app/Config/Autoload.php
@@ -43,6 +43,7 @@ class Autoload extends AutoloadConfig
     public $psr4 = [
         APP_NAMESPACE => APPPATH, // For custom app namespace
         'Config'      => APPPATH . 'Config',
+        'Acme\Blog'   => ROOTPATH . 'acme/Blog',
     ];
 
     /**

Create acme/Blog/Config/Blog.php:

<?php

namespace Acme\Blog\Config;

use CodeIgniter\Config\BaseConfig;

class Blog extends BaseConfig
{
    public $siteName  = 'My Great Site';
    public $siteEmail = '[email protected]';
}

Create app/Config/Blog.php:

<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;

class Blog extends BaseConfig
{
    public $siteName  = 'app/Config/Blog';
    public $siteEmail = '[email protected]';
}

Create acme/Blog/Config/Routes.php:

<?php

$routes->group('blog', ['namespace' => 'Acme\Blog\Controllers'], function ($routes) {
    $routes->get('/', 'Blog::index');
});

Create acme/Blog/Controllers/Blog.php:

<?php
namespace Acme\Blog\Controllers;

use App\Controllers\BaseController;

class Blog extends BaseController
{
    public function index()
    {
        $config = config(\Acme\Blog\Config\Blog::class);
        echo $config->siteName;
    }
}

Run php spark serve.

Navigate to http://localhost:8080/blog

You see app/Config/Blog.

Expected Output

My Great Site

Anything else?

No response

@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 19, 2021
kenjis added a commit to kenjis/CodeIgniter4 that referenced this issue Nov 19, 2021
kenjis added a commit to kenjis/CodeIgniter4 that referenced this issue Nov 19, 2021
@MGatner
Copy link
Member

MGatner commented Nov 20, 2021

This is intentional. Factories with the preferApp flag will always grab a class with the same shortname from app/ if it exists. If an alternate version is needed you can pass preferApp => false to the call.

For Config files specifically I would consider two instances of the same shortname to be a conflict (assuming one does not extend the other) that a developer should resolve.

Modules that need to override or add to known configurations in app/ should use Registrars.

@kenjis kenjis removed the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 21, 2021
@kenjis
Copy link
Member Author

kenjis commented Nov 21, 2021

@MGatner Thank you for your answer.

I did not expect config(\Acme\Blog\Config\Blog::class) may return Config\Blog object.
So I add it to the user guide: #5366

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants