资源控制器上的Laravel路由。使用带有id = create的Show方法而不是create方法

时间:2015-08-26 07:12:54

标签: php laravel routes

以下是Laravel 5.1中的routes.php文件

当我访问/ question / create url时,调用show方法(用于/ question / {id} url)而不是QuestionController中的create方法。

我可能不完全理解routes文件如何解释我在下面的内容。有人可以帮我理解我的文件是如何被解释的以及为什么要调用show方法而不是create方法?

注意:当我没有任何路由组和中间件时,这工作得很好。在我打破它之前,routes文件只是简单地列出了资源控制器(例如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');
        });
    });
});

1 个答案:

答案 0 :(得分:0)

您的资源控制器根据您的路由文件只允许2条路由:index和show。

$routes = ['only' => ['index','show']]; // 

例如,如果此url home/create是您应用上述过滤器的资源控制器的服务器,则将自动触发方法show。如果您需要create方法,只需将其添加到过滤器并运行composer dump-autoload。