0

I have Composer working and I'd like to use its autoloader to load my classes, but it's not working. Here's my directory structure. I'm keeping it really simple to start with.

index.php
composer.json
Vendor
controllers/webgl.php

Inside webgl.php I have:

namespace controllers;

class webgl {
    public function lesson1() {

    }
}

In index.php I have:

require('vendor/autoload.php');
//require_once('controllers/webgl.php');

$webglController = new \controllers\webgl;

And my composer.json defines this for autoloading:

"autoload": { 
    "psr-4": { 
        "controllers\\": "controllers/" 
    } 
}

If I uncomment the second require, the script works, otherwise I get "Fatal error: Class 'controllers\webgl' not found in /vagrant/index.php on line 5".

I thought that the folder structure, class namespace and class name all conformed to psr-4. But I must be misunderstanding something. I've read loads of similar questions but none have been able to sort it for me.

Can anyone tell me why my class isn't loading and what I should do to fix it?

Sven
  • 69,403
  • 10
  • 107
  • 109
tobuslieven
  • 1,474
  • 2
  • 14
  • 21

2 Answers2

1

Did you define an autoload directive?

You need to add this to your composer.json file:

"autoload": {
    "psr-4": {
        "controllers\\": "controllers/"
    }
}

to point the autoloader in the right direction and then run

composer update  

from the terminal in your project directory. Now the class will load without explicitly requiring its file.

tobuslieven
  • 1,474
  • 2
  • 14
  • 21
John Cartwright
  • 5,109
  • 22
  • 25
  • I've tried but am unsure what it should be as the namespace prefix already matches with the folder name. I tried leaving them blank and I've tried one like this: "autoload": { "psr-4": { "controllers\\": "controllers/" } } But neither seemed to work. Any ideas? – tobuslieven May 26 '15 at 20:25
  • Hi I added the details of what I had to do as an edit to your answer. Hope that's not out of order. I'm newish here. I'll accept the answer if the edit is ok'ed. Cheers : ) – tobuslieven May 26 '15 at 22:10
  • @tobuslieven Sorry to jump right in here, but I have suggestions for your questions. First: Add all information you have regarding to the problem. You already HAD something in your composer.json, but it was missing in your question. Answers will probably be better suited to the problem if they can explain you the error in your understanding instead of explaining how things should be without knowing you came to the same result, missing the fact that only a small detail was omitted. Second while I think editing an answer generally is good, an answer that should be a comment likely is deleted. – Sven May 26 '15 at 22:32
  • @tobuslieven I've approved your edit. John, feel free to roll back if you think it's inappropriate. – beaker May 26 '15 at 22:58
  • @sven I didn't have that section in my composer.json at the time I asked the question. I'd tried a bunch of stuff earlier but none of it worked so I had removed it. I tried adding it back in after reading john's answer and finally got it sorted after reading another tutorial offsite. I appreciate your answer very much and voted it up. – tobuslieven May 26 '15 at 23:20
1

Make sure you run at least composer dump-autoload after making changes to your composer.json. composer install or composer update will also do it.

From your question and the comments it seems like you didn't run the command after you added the autoloading definition for your own code.

Sven
  • 69,403
  • 10
  • 107
  • 109