0

I reckon my problem is quite common but even with the mass of doc on namespacing I can't figure out what's wrong with my code.

So I'm using Slim 4.1 and I've put my workfiles in the folder Custom/ in the app/ folder. public/ -- index.php app/ -- Custom/ ---- routes/ ------ manage.route.php -- routes.php src/

I added this line to compose.json.

    "autoload": {
        "psr-4": {
            "App\\": "src/",
            "Custom\\": "app/Custom/" // my custom line
        }
    }

In routes.php I try to call my file using use and the namespace of my files as follow:

use Custom\Routes\Manage as Manage;

and in my class's file:

namespace Custom\Routes\Manage; at its top;

When I hit new Manage(); in routes.php I produce the folling error:

Class 'Custom\Routes\Manage' not found in '[directories]/routes.php'

I'm pretty sure composer loads the files as the number of files autoloaded changes when I remove the namespace calls.

Maybe I'm not calling my Class the right way, I truly don't know at this point. Could you point me the way ?

Thanks a lot in advance

Edits:

I also reloaded composer with composer dump-autoload -o

Composer is adding my folder so the problem isn't from there, it's written in vendor/composer/autoloader_psr4.php 'Custom\\' => array($baseDir . '/app/Custom')

  • 1
    Your namespace should be (I think) `namespace Custom\Routes;` as I think it's creating your class as `Custom\Routes\Manage\Manage` – Nigel Ren Oct 21 '19 at 17:02
  • I can't beleive I wasted hours for something that trivial! I salute you Sir Nigel savior of time. It works. If you want some more reputation you could post a proper answer I'll gladly select it as the answer. Cheers. –  Oct 21 '19 at 17:08
  • Please read https://www.php-fig.org/psr/psr-12/ – Alex Barker Oct 21 '19 at 17:17
  • The funny thing is that I've read it while looking for an answer, I just didn't want to see I guess. I wanted it to be a bigger problem :D Thanks –  Oct 21 '19 at 17:21

1 Answers1

0

As Nigel Ren pointed out it was just a namespace issue.

Your namespace should be (I think) namespace Custom\Routes; as I think it's creating your class as Custom\Routes\Manage\Manage"

Matus Dubrava
  • 13,637
  • 2
  • 38
  • 54