Reputation: 412
Below is my routes.php file in Laravel 5.1
When I access the /question/create url, the show method (meant for /question/{id} url) is called instead of the create method in the QuestionController.
I probably don't fully understand how the routes file interprets what I have below. Can someone help me understand how my file is being interpreted and why the show method is being called instead of the create method?
Note: This was working just fine when I did not have any route groups and middleware. Before I broke it, the routes file just had the resource controllers listed plain and simple (e.g. Route::resource('question','QuestionController'); )
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
//Home Route
Route::get('/','HomeController@getHome');
// Simple Static Routes (FAQ, PRIVACY POLICY, HONOR CODE, TERMS AND CONDITIONS ETC.). No Controller Required.
Route::get('faq',function(){
return view('legal/faq');
});
Route::get('privacypolicy',function(){
return view('legal/privacypolicy');
});
Route::get('honorcode',function(){
return view('legal/honorcode');
});
Route::get('termsandconditions',function(){
return view('legal/termsandconditions');
});
// Authentication routes (Middleware called by Controller)
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');
// Registration routes (Middleware called by Controller)
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');
// Anyone can see the Home Page and Browse Questions, Courses and Universities
$routes = ['only' => ['index','show']];
Route::resource('question','QuestionController',$routes);
Route::resource('course','CourseController',$routes);
Route::resource('university','UniversityController',$routes);
// Routes Protected by Auth Middleware
Route::group(['middleware' => 'auth'],function(){
/*
* Students can view Solutions and Their Profiles
*/
Route::get('solution/{id}','QuestionController@postSolution');
Route::resource('user','UserController',['only' => ['show']]);
/*
* Only Editors have the ability to view the Create Questions Page,
* Store, Edit and Delete Questions that they created, that have not been solved yet
* Create, Edit and Delete Courses and Universities
*/
Route::group(['middleware' => 'editor'],function(){
$routes = ['only' => ['create','store','edit','update','destroy']];
Route::resource('question','QuestionController',$routes);
Route::resource('course','CourseController',$routes);
Route::resource('university','UniversityController',$routes);
/*
* Only Admins have the ability to delete resources
*/
Route::group(['middleware' => 'admin'],function(){
Route::get('admin/execute','AdminController@getExecute');
});
});
});
Upvotes: 4
Views: 4496
Reputation: 4170
I see you have
$routes = ['only' => ['index','show']];
Route::resource('question','QuestionController',$routes);
Route::resource('course','CourseController',$routes);
Route::resource('university','UniversityController',$routes);
Before the group.
Right now because your route show
is before the others like create
It thinks create
is a wildcard of show
. So you should put the lines above at the bottom of the file.
Extra
I notice you have this in your group route
$routes = ['only' => ['create','store','edit','update','destroy']];
Its faster to write it as
$routes = ['except' => ['index','show']];
Except makes sure all routes are available except for the given routes.
And to see which routes are used you can enter the following in your terminal.
Php artisan route:list
Upvotes: 3
Reputation: 4557
Your resource controller has only 2 routes allowed based on your routes file : index and show.
$routes = ['only' => ['index','show']]; //
For example if this url home/create
is server by a resource controller where you applied the above filter, the method show will be fired automatically. If you want the create method, just add it to your filter and run composer dump-autoload.
Upvotes: 0