且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel 5.2路由模型绑定

更新时间:2021-07-15 23:25:43

由于您的变量名为$model,因此Laravel会在网址中查找通配符段,写为{model}:

Because your variable is called $model, Laravel will look for a wildcard segment of the url written as {model}:

在routes.php中:

In routes.php:

Route::get('search/{article}', 'ArticleController@search');

在控制器中:

function search(Article $article) {
    //$article is the Article with the id from {article}, ie. articles/2 is article 2
}

编辑...您建议的方式实际上没有任何意义.那只是一个额外的步骤,仅使用"ArticleController@search"可以完全跳过该步骤.我认为这段代码可以运行,尽管我不建议这样做:

Edit... the way that you are suggesting doesn't really make sense. That would just be an extra step that is skipped entirely by just using "ArticleController@search". I think this code would function although I don't recommend it:

Route::get('search/{article}', function(Article $article)
{
    $controller = App::make(ArticleController::class);
    return App::call([$controller, 'search'], compact('article'));
}