dimanche 31 juillet 2016

Laravel Route Model binding - get model path

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();
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire