Laravel Custom Trait Not Found
Asked Answered
D

9

17

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?

Developer answered 29/5, 2016 at 15:49 Comment(5)
What's the PSR-4 section of composer.json show?Spilt
@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?Developer
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.Spilt
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.Developer
It was the filename of the Trait not matching the classname, in my case...Performative
C
26

Did you dump autoload files?

composer dump-autoload
Crowson answered 29/5, 2016 at 15:52 Comment(4)
better to use php artisan dump-autoload if posibleVoice
he ment composer dump-autoloadLoot
php artisan dump-autoload existed at some point, a comment on one of the answers here says it was deprecated. However, the command still exists on my Laravel 5.5 project.Hedvig
composer dump-autoload --optimize solved my issue, maybe you could consider.Huxley
C
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.

Cristionna answered 12/12, 2016 at 20:10 Comment(0)
P
4

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.

Plemmons answered 11/4, 2018 at 21:13 Comment(0)
S
1
composer dumpautoload -o

this worked for me

Supersaturated answered 4/2, 2021 at 8:13 Comment(0)
K
0

if your file name be Somthing.php and your class or trait or interface name be Something doesn't work . see agian first Something doesn't 'e' .

==> class name must equal to file name

Kamala answered 12/8 at 9:58 Comment(0)
A
-1

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

Aircrewman answered 21/12, 2021 at 11:0 Comment(0)
V
-1

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.

Venom answered 24/2, 2023 at 23:38 Comment(0)
L
-1

I had the same issue after enabling PSR in PHPStorm. The reason was that after I marked my project directory as sources root, IDE auto-filled my usings with lowercase app instead of App (because the directory is app/...)

I had to change:

namespace app\Http\Traits;

to:

namespace App\Http\Traits;
Leeds answered 14/9, 2023 at 14:8 Comment(0)
B
-1

Today I was facing such an issue in my project. after too many attempts I found that I was making a mistake. In which my Traits folder spell was wrong.

On the Trait file, I mentioned that namespace App\Traits; but on the folder, name only trait. that was causing me an issue. so when I fixed that. After that composer dump was successful.

Baeda answered 1/3 at 6:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.