如何设置资源丰富路由的URL?

时间:2016-03-14 13:55:08

标签: php laravel model-view-controller laravel-5

我有一个名为'pages'的资源丰富的路由,带有PagesController和Page模型。我希望show方法可以在http://site.dev/slug而不是http://site.dev/pages/slug上使用。我怎么能在Laravel 5中这样做?

我试过了

Option Explicit
Option Base 1

Dim globalArray(10)

Sub Worksheet_Activate()

Dim i As Long

For i = 1 To 10
    globalArray(i) = Cells(i, 2)
Next i

End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

Dim i As Long

For i = 1 To 10
    If globalArray(i) <> Cells(i, 2) Then
        ' Do stuff here
        '
        globalArray(i) = Cells(i, 2)
    End If
Next i

End Sub

但是这导致以下路线列表:

Route::resource('/{slug}', 'PagesController');

show方法返回错误

|        | POST     | {slug}                         | {slug}.store        | App\Http\Controllers\PagesController@store            |            |
|        | GET|HEAD | {slug}                         | {slug}.index        | App\Http\Controllers\PagesController@index            |            |
|        | GET|HEAD | {slug}/create                  | {slug}.create       | App\Http\Controllers\PagesController@create           |            |
|        | GET|HEAD | {slug}/{{slug}}                | {slug}.show         | App\Http\Controllers\PagesController@show             |            |
|        | PUT      | {slug}/{{slug}}                | {slug}.update       | App\Http\Controllers\PagesController@update           |            |
|        | PATCH    | {slug}/{{slug}}                |                     | App\Http\Controllers\PagesController@update           |            |
|        | DELETE   | {slug}/{{slug}}                | {slug}.destroy      | App\Http\Controllers\PagesController@destroy          |            |
|        | GET|HEAD | {slug}/{{slug}}/edit           | {slug}.edit         | App\Http\Controllers\PagesController@edit             |            |

2 个答案:

答案 0 :(得分:1)

当您使用Route::resource('/{slug}', 'PagesController');时,它将尝试创建多个路由来处理各种RESTful操作。其中一些涉及ID引用的单个对象。例如,&#34; show&#34;方法假设&#34; slug&#34;是描述一组对象的名词,然后期望一个ID来识别一个特定的对象。

我相信你可以通过为该ID使用可选的路由参数来解决这个问题,并选择是否在控制器方法中处理它:Route::resource('/{slug}/{id?}', 'PagesController');

答案 1 :(得分:0)

创建类似于'/ {slug}'的资源路线。

Route::resource('/{slug}', 'MainController');