0

我正在尝试将变量 $id 传递给路由文件中的控制器函数。

以下是各个文件的片段:

路由.php

Route::get('/news-post/{$id}', 'BlogController@getPost');

博客控制器.php

public function getPost($id)
    {
            $post = \App\blog::find($id);
            if($post == NULL){
                App::abort(404);
            }
            return View('webpages.news-post',['title'=>$post['title'],'post'=>$post]);
    }

在 RouteCollection.php 第 161 行收到 NotFoundHttpException:

我已尝试搜索原因,但找不到任何原因。

4

1 回答 1

0

从更改路线

Route::get('/news-post/{$id}', 'BlogController@getPost');

到:

Route::get('/news-post/{id}', 'BlogController@getPost');

去掉路由中id前面的美元符号。查看官方文档中的示例。希望这可以帮助。

于 2016-06-21T21:15:35.123 回答