php - 资源 Controller 上的 Laravel 路由。使用带有 id = create 的 Show 方法而不是 create 方法

标签 php laravel routes

下面是我在 Laravel 5.1 中的 routes.php 文件

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

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

注意:当我没有任何路由组和中间件时,这工作得很好。在我破坏它之前,路由文件只是简单明了地列出了资源 Controller (例如 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 它认为 createshow 的通配符>。所以你应该把上面的行放在文件的底部。

额外

我注意到你在你的组路由中有这个

$routes = ['only' => ['create','store','edit','update','destroy']];  

这样写会更快

$routes = ['except' => ['index','show']];

Except 确保除了给定的路由之外的所有路由都可用。
要查看使用了哪些路线,您可以在终端中输入以下内容。

Php artisan route:list

关于php - 资源 Controller 上的 Laravel 路由。使用带有 id = create 的 Show 方法而不是 create 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32220128/

相关文章:

laravel - 在laravel中写一个查询join sum group by

flutter - 如何在flutter中将文件路由到不同页面

javascript - 如何让我的 if 语句等待 get 请求使用 Angular 完成( GUARD )

php - 在 SQL 数据库中存储 HTML 模板并执行它

javascript - 将变量传递给模态

php - 在 php 中增加不初始化数组值

php - 反序列化时 Laravel 通知失败

php - Mysql查询: filter with condition

php - 如何在 laravel 5 更新期间验证失败后使用最新更改重新填充表单

asp.net-mvc - ASP.NET MVC Url.Action 将当前路由值添加到生成的 url