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

vincej's avatar
Level 15

Error: "Controller Not Defined" ... But I am staring right at it

I don't get it .. I'm staring right at the controller - yet, L5 tells me it is not defined. What dumb mistake might I have done ?

View:

 {!! Form::open( ['action' => 'TestController@index', 'class'=>'form-horizontal']) !!}

Controller:

<?php namespace App\Http\Controllers;

use App\Http\Middleware\Authenticate;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Models\Contractor;
use Laracasts\Flash\Flash;


class TestController extends Controller
{

  public function index()
    {
       return "Hello from Test";
    }

}
0 likes
35 replies
dudod's avatar

Could you provide full exception message

vincej's avatar
Level 15

@bobbybouwmann yup ran composer dump-autoload, made no difference

@dudod

ErrorException in UrlGenerator.php line 576: Action App\Http\Controllers\TestController@index not defined. (View: /var/www/auburntree/resources/views/contractor_portal/portal_dashboard.blade.php)

JeffreyWay's avatar

@vincej - I can't remember, but you may have to specify the full namespaced path to the controller in your form tag.

Laravel provides a short-cut in your routes file, so that you don't have to do that. But I'm not sure if the same is true for form helpers. I'm thinking not.

Snapey's avatar

Check that the filename of the controller has the same casing

jlrdw's avatar

Have you tried it through a route?

vincej's avatar
Level 15

@JeffreyWay

Thanks - but that didn't work either, In fact the 'action' tag fills in the the full App\Http\Controllers for you.

Looking at the Collective docs, their example is:

echo Form::open(array('action' => 'Controller@method'))

Most bizarrely I have other forms which using this syntax and they do work. So I'm a bit confused to say the least.

RachidLaasri's avatar

It randomly happens to me sometimes but not with the form, try to delete and create the class again.

jlrdw's avatar

I like how you titled this post its funny.

vincej's avatar
Level 15

@jimmck

Hi Jim - I just tried it through a route::resource that's what you mean... and I got the same error ... don't you just love computers :o)

Snapey's avatar

Incase you missed it, did you check that you have TestController.php with the same capital letters?

jlrdw's avatar

Yeah this is going to wind up being one of those little tiny errors that you don't spot at first. Usually after taking a break, having a cup of coffee and going back I spot the error.

jlrdw's avatar

If @Snapey was correct, man up and own up to it. We have all done the exact same thing.

jimmck's avatar

@vincej Perhaps this some other way to do it. But an action normally requires a URL. I know is a blade, but first question I ask is how is the Controller being mapped inside the blade. So I mean, did,you user a URL and map it in your route file. Or do you use a JS method and reference the URL there.

jlrdw's avatar

Try a regular get route, add a get method. And does that class you added have anything to do with it?

jlrdw's avatar

Heck I'm fixing to pop some popcorn.

vincej's avatar
Level 15

@jimmck

I have tried it with a route:: resource, a get route, and of course placing the controller@method into the action tag as per Collective's docs. I get the same error every time.

Here is the bizarre thing. If I put into the action tag an existing controller I created, say last week, then everything works well. eg:

 {!! Form::open( ['action' => 'ContractorEditController@confirm', 'class'=>'form-horizontal']) !!}

However, if I put a controller in which I created today, then it does not see it. It's like somehow Laravel does not register my controllers. But they look the same and alright to me.

jlrdw's avatar

Have you done an update recently? And of course you checked all your spelling capitalizations and that sort of thing as @Snapey suggested. Perhaps try another with a different name just to see what happens. I'm going after a second dish of popcorn now.

jlrdw's avatar

You can't buy this kind of entertainment this is a fun one. But we'll get it fixed.

vincej's avatar
Level 15

Yup I have checkEd all the capitalisations about 200 times.

Update .... hmmm of what exactly ? I have done a clear-compile and a composer dump autoload

It just will not see my new controllers. I have tried deleting them and recreating them.

enjoy the popcorn - at this rate I'll be driving myself to drink !

jlrdw's avatar

Well no matter what let us know how you resolved it do not keep us in the dark here. Need some Pepsi now to wash down the popcorn.

vincej's avatar
Level 15

@jlrdw

Sure thing,

I have this new controller in a subdirectory of Controllers ... I wonder if that is part of the problem. But when I created a TestController in the main directory I had the problem.

Snapey's avatar

so what's the path to your controller, because it should be the same as the namespace you declared?

dmcglone's avatar

@vincej

"I have this new controller in a subdirectory of Controllers ... I wonder if that is part of the problem. But when I created a TestController in the main directory I had the problem."

So, should your example controller namespace look like this:


<?php namespace App\Http\Controllers\<your subdirectory>;

dmcglone's avatar

I just created a App/Controllers/Test/TestController.php

<?php namespace App\Http\Controllers\Test;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class TestController extends Controller {

    public function index()
    {
        return "hello world";
    }

Then added this route:

Route::get('test', 'Test\TestController@index');

And it works, but if I remove either or both Test\ from the route and/or controller I get the same error as you.

Hope that helps.

irfanm's avatar

Found this post through Google, and although it's old, my issue wasn't with namespaces. Even though I was using Controller@method, I forgot to create a corresponding route that uses Controller@method. Seems obvious when it works, hope it helps someone!

Next

Please or to participate in this conversation.