I'm using Laravel 5.1's route model binding to infer/instantiate the correct model instance from the routes.
I don't want to rewrite a show() method for each resource, and would rather call parent show() method in a generic parent class. The problem is, I don't know how to allow my parent class's show() method to infer the correct model instance, so I'm forced to create a show() on each child resource and specify the model (like below, User $user).
How can I get the bound model in my controllers from the binding in RouteServiceProvider: App\User? And pass it through to the parent show() method?
Route: $router->get('/users/{user}', 'Resources\Users@show');
RouteServiceProvider.php:
public function boot(Router $router)
{
parent::boot($router);
$router->model('user', 'App\User');
}
UsersController.php:
class User extends ResourceController
{
public function show(Request $request, User $user)
{
return $user;
}
ResourceController.php:
class ResourceController extends Controller
{
public function show(Request $request, Model $model)
{
// Infer "Users" model here and get correct instance
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire