The route model binding is handled from the routes file, not from the controllers themselves..
# routes.php
// Name your variable placeholders nicely so you don't get collisions between concerns.
Route::get('/site/details/{site_id}', [ 'as' => 'site.details', 'uses' => 'SiteController@details', ]);
# RouteServiceProvider.php
public function boot(Router $router)
{
// You bind the App\Site::find('{site_id}') from your route.
// If you don't define the route explicitly - as with controller routing - this isn't going to work
// because the placeholder ({site_id}) has not been defined anywhere.
$router->model('site_id', Site::class);
}
# SiteController.php
use App\Site;
// ...
public function details(Site $site)
{
return $site;
}
It's probably best to avoid implicit routing, given you're only going to run into more complications as your application grows.
As for routing individual get or post methods, you can group concerns to cut down on keystrokes a bit, but being explicit helps to use your routes file as something of documentation for your application.
# routes.php
Route::group([ 'prefix' => 'site', 'as' => 'sites::', ], function () {
// route('sites::store');
// POST /sites
post('/', [ 'as' => 'store', 'uses' => 'SiteController@store', ]);
// route('sites::create');
// GET /sites/create
get('create', [ 'as' => 'create', 'uses' => 'SiteController@create', ]);
Route::group([ 'prefix' => '{site_id}', ], function () {
// route('sites::update', $id);
// PUT /sites/{site_id}
put('/', [ 'as' => 'update', 'uses' => 'SiteController@update', ]);
// route('sites::delete', $id);
// DELETE /sites/{site_id}
delete('/', [ 'as' => 'delete', 'uses' => 'SiteController@delete', ]);
// route('sites::details', $id);
// GET /sites/{site_id}/details <- (subjectively) better as you can group routes more easily
get('/details', [ 'as' => 'details', 'uses' => 'SiteController@details', ]);
});
});
Now, if you're being explicit with your application routes in your routes.php file, you can see which routes are set for your whole application, without trawling through your app controllers to see what mystery routes lie therein.