Download the PHP package dgoring/laravel-inherit-resource without Composer
On this page you can find all versions of the php package dgoring/laravel-inherit-resource. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-inherit-resource
Laravel Inherit Resource
Inspired by the ruby gem InheritResource
I bring you quick minimalist resource controllers in laravel
Basic usage
Simply place resource trait in your controller
with the resource route in your routes file
And all the resource functions are placed and run against the class as well as loading the views
all settings are assumed by the name of the controller so UsersController
is assumed to use
App\User
for the model classuser
the route parameter- these views are assumed for there respective resource functions
--
users.index
with$users
passed into it as a paginator collection --users.show
with$user
passed into it as the instance of the record --users.create
with$user
a fresh new instance of the model --users.edit
with$user
passed into it as the instance of the record
the query
To override the query used for say filtering the index page
you can override the collection
function
The collection
function is also used for all resource functions to give the base query
So can be used to filter content across the controller
Authorized Resource
By default all the actions are put through the authorize function, so you can control access to this resource
index
->authorize(viewAny
, class)create
->authorize(create
, new instance)store
->authorize(create
, new instance)edit
->authorize(update
, instance)update
->authorize(update
, instance)destroy
->authorize(delete
, instance)
Validation rules
You can specify validation rules for the store
and update
functions but defining validationRules
function
nested resource
Nested resources are just as easy
Simply setup your route
And return the relationship in the collection function
as the collection function is used for the create
and store
functions
as long as your return the relationship from the parent model the relationship should be saved to the new record
and it should also already be in the model in the create
view variable
JSON
this resource is also ready to respond to json requests
note that the index will return a total count in the response headers as Count
also if defined in a appropriate place a JsonCollection or JsonResource formatter will be used for the model i.e.
And you can also allow only JSON responses
or only HTML
Overrides
Namespaces
By default models are assumed to be under the App
namespace but you can change that by adding the file config/inherit_resource.php
below shows the config file for App\Models
and also the namespaces for the json resources and collections
Controller variables
Responses
these response functions can be overridden to allow you to have completely custom responses to certain actions without having to override the entire route function
class UsersController extends Controller
{
use Resource;
protected function htmlIndex();
protected function htmlShow();
protected function htmlCreate();
protected function htmlStoreSuccess();
protected function htmlStoreFailure();
protected function htmlEdit();
protected function htmlUpdateSuccess();
protected function htmlUpdateFailure();
protected function htmlDestroySuccess();
protected function htmlDestroyFailure();
protected function jsonIndex();
protected function jsonShow();
protected function jsonStoreSuccess();
protected function jsonStoreFailure();
protected function jsonUpdateSuccess();
protected function jsonUpdateFailure();
protected function jsonDestroySuccess();
protected function jsonDestroyFailure();
}