I'm trying to use Laravel 5.1 Route Model binding to simplify my resource controllers.
I have a model Categories
, which inherits from ResourceController
, which attempts to list generic methods like index
, show
, etc.
I know you can use route binding to guess a model instance based on a route variable, such as {category}
would get me a specific category ID.
But from the route below, how can I get Resources\Categories
passed into the parent ResourceController index method?
Route:
$router->get('/categories', 'Resources\Categories@index');
Categories controller for model Category:
class Categories extends ResourceController {
Parent ResourceController: I am manually trying to grab the model path, /categories
and convert it to App\Category
, but this seems like the wrong way to do it.
class ResourceController extends Controller
{
public function index()
{
// /categories
$route_uri = Route::getFacadeRoot()->current()->uri();
// Do some string conversion to get `App\Category` from `/categories` route
$model = stringConverter($route_uri);
// Then return App\Category::all()
return $model::all();
}
I suppose another option is explicitly passing in the class to the parent. But this seems unnecessary since I already have this information from the route:
class Categories extends ResourceController
{
public function index()
{
return parent::index(Category::class);
}
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2akBRzk
via IFTTT
Aucun commentaire:
Enregistrer un commentaire