Laravel相互冲突的路线

My routes.php excerpt:

Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function() {

    Route::resource('posts', 'PostsController', [
        'except' => ['show']
    ]);

    Route::delete('posts/trash', [
        'as' => 'posts.trash.empty',
        'uses' => 'PostsController@emptyTrash'
    ]);

});

My PostsController.php excerpt:

/**
 * DELETE /admin/posts/{id}
 */
public function destroy($id)
{
    // code
}

/**
 * DELETE /admin/posts/trash
 */
public function emptyTrash()
{
    // code
}

The problem is that Laravel confuses the 'trash' string in a DELETE /admin/posts/trash request as an {id} parameter. As a consequence, the destroy() method is called instead of emptyTrash(). Why and What can I do for this?

Firstly, order matters. Laravel will search the routes for a match in the order you register them. As you figured out, Laravel will take trash as an id and therefore the URI matches the resource route. Since that route is registered before your additional one, it will use the resource route.

The simplest fix is to just change that order:

Route::delete('posts/trash', [
    'as' => 'posts.trash.empty',
    'uses' => 'PostsController@emptyTrash'
]);

Route::resource('posts', 'PostsController', [
    'except' => ['show']
]);

If you don't like that you can try to limit the parameter for your resource route to numbers only. Unfortunately you can't just add a ->where() to the resource route like you could with others.

Instead you have to define a global pattern for the route parameter. The route parameter Route::resource chooses is the resource name (in snake_case).

So this should work for you:

Route::pattern('posts', '[0-9]+');

Somewhere in your view, you should have a button or a link for actually deleting the post. The view should look something like this:

@section('content')
<div class="panel panel-default">
    <div class="panel-heading clearfix">
        <b>{{ $post->post_name . ' (id:' . $post->post_id . ')' }}</b><br />
        <b> {{ link_to_route('overview', 'Go Back To Post List') }}   </b>
        <div class="pull-right">
            // FORM FOR DELETING POST
            {{ Form::open(array('route' => array('delete_post', $post->post_id))) }}
                {{ link_to_route('edit_post', 'Edit Post', array('id' => $post->post_id), array('class' => 'post_img_button_edit')) }}
                {{ Form::hidden('_method', 'DELETE') }}
                {{ Form::submit('Delete Post', array('class' => 'post_img_button_delete')) }}
            {{ Form::close() }}
        </div>

      <div class="pull-right">
            // FORM FOR EMPTYING TRASH
            {{ Form::open(array('route' => 'empty_trash')) }}
                {{ Form::hidden('_method', 'DELETE') }}
                {{ Form::submit('Empty Trash', array('class' => 'post_img_button_delete')) }}
            {{ Form::close() }}
        </div>

    </div>

/* Additional HTML code within view */

Your controller should be similar to this:

public function destroy($id)
{
    $this->post->delete($id);
    return \Redirect::route('overview'); 
}

public function emptyTrash()
{
  // code for collecting and emptying Trash
}

And your routes should look similar to this:

Route::delete('admin_posts/admin_posts/{id}/destroy', array('as'=>'delete_post', 'uses'=>'PostsController@destroy'));

Route::delete('posts/trash', array('as'=>'empty_trash', 'uses'=>'PostsController@emptyTrash'));

The name of your route for actually deleting posts be 'delete_post'.

The name of your route for emptying your trash will be empty_trash

Basically you're explicitly defining your routes so that you'll avoid less ambiguity and Laravel will know which routes to take. Hopefully this information will help!