VenoM
VenoM

Reputation: 463

Route [controller.create] is not defined when I've already defined the Resource Controller in my Web.php

So I'm working with two Resource Controllers (I'm not sure if that's the problem but if it is, please let me know); one of them is StudentController and its URL is "/main". It's working as it should, perfectly. Now I'm creating the 2nd Resource Controller which name is TeacherController, and its URL is "teacher/main" - i.e its files are in the 'teacher' folder. This is my web routes file:

Web.php:

    Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::resource('/main','StudentController');

Route::get('profile','ProfileController@index')->name('profile');

Route::get('profile/edit','ProfileController@edit')->name('profile/edit');

Route::put('profile/edit/{id}','ProfileController@update')->name('profile.update');

Route::resource('teacher/main','TeacherController');

Now when I go to teacher/main - it says that "teacher.main.create" is not defined. That is the route that I've used on my create button i.e

{{ link_to_route('teacher.main.create','Add a Teacher','',['class'=>'btn btn-success float-right']) }}

And this is the create() function in my Resource Controller:

public function create()
{
    return view ('teacher/create');
}

From my understanding of Laravel Resource Controllers, I shouldn't have to define each Resource Controller function individually as Laravel should automatically detect each of them since I've already defined Route::resource('teacher/main','TeacherController'); in my Web Routes. What am I doing wrong here? This is just the first function it's getting, then there are other functions like Edit/Destroy that will also pop-up route errors and I don't want to go through with defining each of them individually. Help is highly appreciated.

Upvotes: 0

Views: 317

Answers (3)

zlatan
zlatan

Reputation: 3951

I just tested this locally, and seems like you're using wrong name for your resource route. I set the routes exactly as in your example, and this is what I got:

enter image description here

So if you wanted to use the create route, you'd do it like this:

{{ link_to_route('main.create','Add a Teacher','',['class'=>'btn btn-success float-right']) }}

On the other hand, if you want to keep the names you wanted, you can set a custom name for each of your resource route, like this:

Route::resource('teacher/main', 'TeacherController', [
    'names' => [
        'index' => 'teacher.main.index',
        'create' => 'teacher.main.create',
        // etc...
    ]
]);

Hope this helps. Happy coding.

Upvotes: 1

albus_severus
albus_severus

Reputation: 3704

Route::resource('teacher-main','TeacherController');

now you can access

teacher-main.index

Upvotes: 0

Dilip Hirapara
Dilip Hirapara

Reputation: 15296

Try to use in group with prefix and as

Route::group(['prefix' => 'teacher','as'=>'teacher.'], function () {
    Route::resource('main','TeacherController');
});

Now you can use teacher.main.create

{{ link_to_route('teacher.main.create','Add a Teacher','',['class'=>'btn btn-success float-right']) }}

Upvotes: 1

Related Questions