0

I am using slim framework, composer, and psr-4 autoload.

This is in composer:

"Shorty\\":"app/Shorty"

Note: I tried the above also with "Shorty\\":"app/Shorty/Models"

directorry structure: app/Shorty/Models/Trap.php

Inside Trap.php:

namespace Shorty\Models;

use Illuminate\Database\Eloquent\Model as Eloquent;

    class Trap{
    #code here
    }

In my route:

$users=Trap::leftJoin('users', function($join){

and I get: Class 'Trap' not found

What did I do wrong?

Wouter J
  • 41,455
  • 15
  • 107
  • 112
TDawg
  • 833
  • 2
  • 8
  • 24
  • This may not help, but have you run a `composer dump-autoload` (or a composer install / update) since adding your psr-4 namespace? – Ross Jul 04 '15 at 21:38
  • I did the dump-autoload, several times too – TDawg Jul 05 '15 at 15:35
  • Then otherwise ensure you have a `use Shorty\Models\Trap;` declaration in the top of your route file where you're using `Trap`. If that's there, I can't see anything that shouldn't work (the `"Shorty\\":"app/Shorty"` part should work). Perhaps you can expand your question with the full copy and paste of your composer psr-4 declaration. If there's any cache you can clear in your app, try that too. – Ross Jul 05 '15 at 15:54
  • I had use Shorty\Models\Trap; in a global include file, I now tried moving it to my main routes file. Below is the composer code... "autoload":{ "psr-4":{ "Shorty\\":"app/Shorty" } }, – TDawg Jul 05 '15 at 16:09
  • found this on stackoverflow: http://stackoverflow.com/questions/25499637/laravel-psr-4-not-autoloading do I need a classmap? the tutorial I followed didn't use one... – TDawg Jul 05 '15 at 16:11
  • I just tried creating a brand new laravel project and modified the composer autoload (while leaving the laravel defaults in place) to include the line you gave and created an empty class Trap at the location you mention in your question. As far as I can determine, your fundamentals are there and working. Sorry I can't help more, it appears to be something more involved. – Ross Jul 05 '15 at 17:54

1 Answers1

0

If the class name would have been correct, you wouldn't receive this error message:

Class 'Trap' not found

but this

Class 'Shorty\Models\Trap' not found

Not mentioning the fully qualified class name including it's namespace tells me that your code that is missing the class neither has a namespace statement, nor does an import with use, to import this "Trap" class.

Or to be more precise:

This code will complain about a missing "Trap" class.

Trap::leftJoin();

This code would complain about a missing "Shorty\Models\Trap" class.

\Shorty\Models\Trap::leftJoin();

As well as this one:

use Shorty\Models\Trap;

Trap::leftJoin();

Or this one:

namespace Shorty\Models;

Trap::leftJoin();

When PHP complains about a class not being present, it always displays the fully evaluated, final name of that class, after aliases, relative namespace indirections and stuff.

Sven
  • 69,403
  • 10
  • 107
  • 109
  • Sven, Wouter J, Ross thank you all. I am a bit new to this way of doing things, I made another assumption which I realize now was wrong. The tutorial I followed had a routes.php file, with includes of indicudual route files in a subfolder. I had the use comment in the routes.php, (I ha also tried in the start.php include file), it worked only after moving the use to the actual included file, which is routes/home.php in this case. Perhaps I should have posted my entire directory structure. You all provided amazing help. Thanks again. – TDawg Jul 06 '15 at 07:23