Laravel路由模型绑定到路由::controller()


Laravel Route Model Binding to Route::controller()

我有以下路线:

Route::controllers([
    'auth' => 'Auth'AuthController',
    'password' => 'Auth'PasswordController',
    'admin' => 'AdminController',
    'site' => 'SiteController'
]);

然后我在SiteController中得到了以下方法:

/**
 * Get site details and pass to view
 *
 * @param Site $site
 * @return mixed
 * @internal param $site_id
 */
public function getDetails( Site $site )
{
    return $site;
}

当我转到URL site.com/site/details/13时,它不会返回站点对象。

我已经在RouteServiceProvider中添加了$router->model( 'one', 'App'Site' );,它很有效,但如果以后我想添加另一个这样的控制器,但将其用于jobs,并再次使用getDetails方法并通过App'Job对象,该怎么办?它将自动发送App'Site型号。

那么,我有办法防止这种情况发生吗?

我对Laravel的有限知识告诉我,你不能在你的路线控制器函数中使用模型/对象作为参数,而且你不需要像$router->model( 'one', 'App'Site' );这样的东西来做到这一点。

我想你会想做这样的事情:

至于您的路线:

Route::controllers([
    'auth' => 'Auth'AuthController',
    'password' => 'Auth'PasswordController',
    'admin' => 'AdminController',
    'site' => 'SiteController',
    'jobs' => 'JobController',
]);

在您的SiteController:中

use Illuminate'Http'Request;
use App/Site; //replace with namespace of model    
public function getDetails($id)
{
    //code for fetching the site object, depends on how your structure is, 
    //like $site = App'Site::find($id); etc
    return $site;
}

类似地,您的JobController将类似于:

use Illuminate'Http'Request;
use App/Job; //replace with namespace of model    
public function getDetails($id)
{
    //code for fetching the job object, depends on how your structure is, 
    //like $job = App'Job::find($id); etc
    return $job;
}

查看此处:Laravel Docs-隐式控制器