【问题标题】:Error in passing argument to a controller function in laravel在 laravel 中将参数传递给控制器​​函数时出错
【发布时间】:2016-06-21 20:26:43
【问题描述】:

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

以下是各个文件中的 sn-ps:

Routes.php

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

BlogController.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:

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

【问题讨论】:

  • 请检查下面的答案。希望它有效

标签: php laravel laravel-5 routes httpexception


【解决方案1】:

改变路线

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

到:

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

去掉路由中id前面的美元符号。查看official documentation 中的示例。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 2016-05-16
    • 2017-01-12
    • 2018-07-14
    相关资源
    最近更新 更多