Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PotTime's avatar

Controller not found

Hello, I have a problem: my controller AvatarController is not found. His namespace is App\Http\Controllers\User.

I have tried to use him with action('AvatarController@method') and action('User\AvatarController@method').

But with them, I get the error: Action App\Http\Controllers\AvatarController@getUserAvatar not defined. and Action App\Http\Controllers\User\AvatarController@getUserAvatar not defined.

Thanks

0 likes
20 replies
PotTime's avatar

Here is my action:

<img src="{{ action('User\AvatarController@getUserAvatar', ['name' => $name]) }}" />

And here is my AvatarController:

class AvatarController extends Controller
{

    protected function getUserAvatar($name)
    {
        try {
            $user = (new User)->where('name', '=', $name)->firstOrFail();
        } catch (ModelNotFoundException $exception) {
            return $this->getRandomAvatar();
        }

        return 'storage/' . $user->avatar;
    }
}
Snapey's avatar

Add the namespace lines to the post above so we can see

What is the folder structure and file name?

Snapey's avatar

Your function is protected not public. Protected functions are only visible to same class or a class that extends it.

PotTime's avatar

Avatar Controller with the namespace (without uses):

namespace App\Http\Controllers\User;

class AvatarController extends Controller
{

    protected function getUserAvatar($name)
    {
        try {
            $user = (new User)->where('name', '=', $name)->firstOrFail();
        } catch (ModelNotFoundException $exception) {
            return $this->getRandomAvatar();
        }

        return 'storage/' . $user->avatar;
   

And here is the structure: http://prntscr.com/k3wmiu

Snapey's avatar

Change

    protected function getUserAvatar($name)

to

    public function getUserAvatar($name)
PotTime's avatar

This is the same error.. Action App\Http\Controllers\User\AvatarController@getUserAvatar not defined.

Snapey's avatar

Have you run composer dump recently?

Snapey's avatar

you need to run it if you change the name or namespace of a class

PotTime's avatar

After the command, I have the same error... Inside my IDE, I need to redownload all files?

PotTime's avatar

I call my controller inside a blade file. Is it a problem?

Snapey's avatar

no, using action inside blade is not a problem

you changed protected to public?

What is your current error message?

PotTime's avatar

I have my function from protected to public. My error message is Action App\Http\Controllers\User\AvatarController@getUserAvatar not defined. (View: /var/www/html/resources/views/emails/verifyuser.blade.php)

Snapey's avatar
Snapey
Best Answer
Level 122

Do you have a route corresponding to that action?

1 like
Snapey's avatar

its not possible to create action in your blade if there is no route to map to

1 like

Please or to participate in this conversation.