15

I'm new to traits, but thought I'd give it a try. But, it doesn't seem to load.

I've created a trait within a folder under the Laravel app directory: app\Helpers called CheckPermsAgainstObjectTrait.php

Here is the trait code:

<?php
namespace App\Helpers;

trait CheckPermsAgainstObjectTrait {
    function something{}
}

I try to use it in a controller as such:

<?php

namespace App\Http\Controllers;

use this&that;
use App\Helpers\CheckPermsAgainstObjectTrait;

class PolicyController extends Controller{

   use CheckPermsAgainstObjectTrait;
}

Classes in that directory load fine. PHPStorm sees the trait fine. I've clear compiled aritsan and dumped autoload. I'm guessing there is something that Laravel doesn't like with the namespacing? I would hope I don't need to do any manual loading in composer -- but I'm having trouble finding any documentation to give me a hint as to what I'm screwing up.

The error:

FatalErrorException in PolicyController.php line 15: 
Trait 'App\Helpers\CheckPermsAgainstObjectTrait' not found

Any thoughts?

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
Watercayman
  • 7,970
  • 10
  • 31
  • 49
  • What's the PSR-4 section of composer.json show? – Devon Bessemer May 29 '16 at 15:56
  • @Devon PSR-4 is just the standard Laravel: "App\\": "app/" I can't find mention of needed to load a trait differently than a class -- is that the case? – Watercayman May 29 '16 at 16:01
  • 3
    No, that should be fine. Traits, classes, interfaces should all work the same with composer. I'm assuming there is a typo somewhere, possibly in the filename. Just make sure app/Helpers/CheckPermsAgainstObjectTrait.php does properly exist. – Devon Bessemer May 29 '16 at 16:02
  • 1
    Gah!! That was it! Wow, I had to go to SO to get help figuring out a typo. The class was named Ojbect. @Devon, thank you. – Watercayman May 29 '16 at 16:08
  • It was the filename of the Trait not matching the classname, in my case... – burf Jul 11 '22 at 10:51

7 Answers7

25

Did you dump autoload files?

composer dump-autoload
huuuk
  • 4,597
  • 2
  • 20
  • 27
  • 1
    better to use `php artisan dump-autoload` if posible – dani24 May 05 '17 at 17:21
  • he ment composer dump-autoload – Shamseer Ahammed Jun 04 '19 at 06:40
  • 1
    `php artisan dump-autoload` existed at some point, a comment on one of the answers [here](https://stackoverflow.com/questions/20274082/what-are-the-differences-between-php-artisan-dump-autoload-and-composer-dump) says it was deprecated. However, the command still exists on my Laravel 5.5 project. – Skeets Jul 03 '19 at 03:30
  • 3
    `composer dump-autoload --optimize` solved my issue, maybe you could consider. – bencagri Sep 17 '19 at 07:29
16

The answer for me was that I had the wrong namespace at the top of my trait file due to working from a Laravel trait as an example. If your trait is in App/Traits/MyTrait.php then make sure your namespace is:

namespace App\Traits;

trait MyTrait
{
    // ..
}

Then from the file that's including the trait:

use App\Traits\MyTrait;

class MyClass
{
    use MyTrait;

    // ..
}

No need to mess with config/app.php, composer.json or autoloading.

Zack Morris
  • 4,727
  • 2
  • 55
  • 83
3

I had a very similar problem where I was getting the error Trait 'Tests\CreatesApplication' not found in '.../my_project/tests/TestCase.php' on line 9 while running tests with phpunit because I manually updated my Laravel project and must have missed some changes. This may also happen to anyone creating a trait or class in a unique namespace.

Anyways, @Devon 's comment on the original question pointed me in the right direction.

I was missing the required autoloader configuration in my composer.json file, so I checked what it should be for my version of Laravel (5.4 in this case) from the Laravel github repository.

In this case, I was not calling from a namespace within App\ (which was already in composer.json.)

Here's what I was missing from composer.json:

{
    ...,
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    ...
}

Thanks goes to Devon! Hope this keeps someone else from pulling their hair out.

Quisk
  • 71
  • 5
2
composer dumpautoload -o

this worked for me

Ranjith
  • 61
  • 6
0

This may happen sometime when your Trait filename is misspelled or incorrect as with the class name. Do check your file name is same as classname

Abhi Burk
  • 2,003
  • 1
  • 14
  • 21
0

This may happen sometime when your Trait filename is misspelled or incorrect as with the class name. Do check your file name is same as classname

joo
  • 1
0

If you copied and pasted the trait, then it will likely not work, what you do, delete the trait folder and create it manually, then delete the trait class and create it manually. (By typing), then make sure the first letter of your class starts with a capital letter. There it must work.

MUHINDO
  • 788
  • 6
  • 10