Laravel 资源控制器上的路由。使用带有 id = create 的 Show 方法而不是 create 方法
Laravel Routes on Resource Controller. Using Show method with id = create instead of the create method
下面是我在 Laravel 5.1
中的 routes.php 文件
当我访问 /question/create url 时,将调用 show 方法(用于 /question/{id} url)而不是 QuestionController 中的 create 方法。
我可能不完全理解路由文件如何解释我下面的内容。有人可以帮助我了解我的文件是如何被解释的,以及为什么调用 show 方法而不是 create 方法吗?
注意:当我没有任何路由组和中间件时,这工作得很好。在我破坏它之前,路由文件只是简单明了地列出了资源控制器(例如 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');
});
});
});
我看到你有
$routes = ['only' => ['index','show']];
Route::resource('question','QuestionController',$routes);
Route::resource('course','CourseController',$routes);
Route::resource('university','UniversityController',$routes);
组前
现在因为你的路线 show
在其他人之前 create
它认为 create
是 show
的通配符。所以你应该把上面的行放在文件的底部。
额外
我注意到你在你的组路由中有这个
$routes = ['only' => ['create','store','edit','update','destroy']];
写成这样比较快
$routes = ['except' => ['index','show']];
Except 确保除给定路线之外的所有路线都可用。
要查看使用了哪些路线,您可以在终端中输入以下内容。
Php artisan route:list
根据您的路由文件,您的资源控制器只允许 2 条路由:index 和 show。
$routes = ['only' => ['index','show']]; //
例如,如果此 url home/create
是您应用上述过滤器的资源控制器的服务器,则显示方法将自动触发。如果你想要创建方法,只需将它添加到你的过滤器和 运行 composer dump-autoload。
下面是我在 Laravel 5.1
中的 routes.php 文件当我访问 /question/create url 时,将调用 show 方法(用于 /question/{id} url)而不是 QuestionController 中的 create 方法。
我可能不完全理解路由文件如何解释我下面的内容。有人可以帮助我了解我的文件是如何被解释的,以及为什么调用 show 方法而不是 create 方法吗?
注意:当我没有任何路由组和中间件时,这工作得很好。在我破坏它之前,路由文件只是简单明了地列出了资源控制器(例如 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');
});
});
});
我看到你有
$routes = ['only' => ['index','show']];
Route::resource('question','QuestionController',$routes);
Route::resource('course','CourseController',$routes);
Route::resource('university','UniversityController',$routes);
组前
现在因为你的路线 show
在其他人之前 create
它认为 create
是 show
的通配符。所以你应该把上面的行放在文件的底部。
额外
我注意到你在你的组路由中有这个
$routes = ['only' => ['create','store','edit','update','destroy']];
写成这样比较快
$routes = ['except' => ['index','show']];
Except 确保除给定路线之外的所有路线都可用。
要查看使用了哪些路线,您可以在终端中输入以下内容。
Php artisan route:list
根据您的路由文件,您的资源控制器只允许 2 条路由:index 和 show。
$routes = ['only' => ['index','show']]; //
例如,如果此 url home/create
是您应用上述过滤器的资源控制器的服务器,则显示方法将自动触发。如果你想要创建方法,只需将它添加到你的过滤器和 运行 composer dump-autoload。