Skip to content

Instantly share code, notes, and snippets.

@deleugpn
Last active July 7, 2016 13:56
Show Gist options
  • Save deleugpn/ff86eb58f345664f51bfeed88f0e7c32 to your computer and use it in GitHub Desktop.
Save deleugpn/ff86eb58f345664f51bfeed88f0e7c32 to your computer and use it in GitHub Desktop.
Dynamic Dependency Injection - MAKE SURE YOUR MODEL IMPLEMENTS ModelInterface!!
<?php
namespace App\Http\Controllers;
use App\ModelInterface;
use Illuminate\Http\Request;
use App\Http\Requests;
class CrudController extends Controller {
protected $model;
/**
* CrudController constructor.
* @param $model
*/
public function __construct(ModelInterface $model) {
$this->model = $model;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
return $this->model->all();
}
}
<?php
namespace App;
interface ModelInterface {
}
<?php
Route::get('/{model}', function($model){
// Build App Object
$app = app();
// Bind Dynamic DI
$app->bind(\App\ModelInterface::class, '\\App\\' . $model);
// Run controller and method
$controller = $app->make(\App\Http\Controllers\CrudController::class);
return $controller->callAction('index', []);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment