Download the PHP package bfg/resource without Composer

On this page you can find all versions of the php package bfg/resource. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package resource

Extension resource

Install

Description

A small addition to the resources to the Laravel, which is a dumb modifiable due to the overridency of the main team of the Laravel to the new Stub in which another resource inheritance is included which, in turn, everything is also inherited from Illuminate\Http\Resources\Json\JsonResource so that the reasons of the Laravel will understand it as a full-fledged resource.

About the concept.

The concept of expansion is to reduce resources and make them more flexible and versatile. The flexibility lies in the fact that each field is to have an independent additional possibility of obtaining data on paths and the use of the Laravel Eloquent Casts and Laravel Eloquent Mutators. Also knows how to determine the relationship of models and connect the fields when they are loaded. Rule for pagination.

Possibilities

The possibilities of this package in aggregate with the bfg/route pack allow me to organize a not big but very powerful pattern for api that can compete with convenience with the GraphQL.

Where can I use it?

This package is intended only in order to make an add-in over the framework Laravel. With the full use of the package, it is rapidly back and efficiently implement a resistant API resource for models and not only.

Create new resource

In order to create a resource, it is enough to use the standard Command:

I added some properties there:

After execution, the app/Http/Resources/UserResource.php file will be created with the following contents:

Further, all we need is to fill out a resource map.

Mutators

For example, in the case of the user, for its photo we need to process the data for this field and for it we can use the mutator for it. All mutators are determined by the same rules as in the Laravel models only instead of Attribute write Field.

Casting

All resource casting rules are completely copied from the casting of Laravel Attributes and their functionality is absolutely identical (except for the set of custom casts):

Extending (Re-use)

Combines the results of resource fields. Performed before the main resource for redefining the parent.

Important! Common resources should work with one model.

Routing

To use resources as API Controllers, I recommend that you use Laravel Fortify or JetStream as an API provider.

Definition of routs

Before you start, you need install the package bfg/route:

In your RouteServiceProvider Add the pointer to search for routs:

Then you need to add an attribute to the resource class:

After that, you will appear in the route link api/user/{scope} that refers to this resource.

Scopes in routes

All resource skys should be static and public. By linking the parameters of scopes must be understood as they work. The idea of a sequential call of scopes is as follows: The sequence of them is fixed sequentially through the slash in the query reference, for example:

In the result you will receive a list of users without any fields as Only accepts the parameters, namely the fields that need to be displayed. In order to transfer the fields in scope, we use the same syntax as the sequential call only after each scope goes its parameters, the resource controller processes the string sequentially and if scope is found in this sequence, it needs to be performed. All other parameters after it are considered to be the parameters of the scope if the name of the other (next) scope does not occur in these names and so to the end.

This is how the request for obtaining data with pagination and field filtering is:

Important! Your resource must be connected to the trait Bfg\Resource\Traits\EloquentScopesTrait and Bfg\Resource\Traits\ModelScopesTrait

Each of each scope is single for each pre request, on thescope

After pagination, the collection also returns, and in this case, each of its recording is processed only scope.

Fields that have not entered the resource after scope only Fill the null

Put a question mark ? In front of the field name in the resource map:

Fill in the resource an array with fields that must be temporary:

Put the trigger to true, which will make temporary all resource fields:

Scopes in resource

In the class of the Scopes resource look like public static functions with the name in CamelCase and postfix Scope

Consider what the standard Resource Eloquent Get Scope:

The first parameter in Scope goes the current model that came either from the previous scope either the standard resource model that will try to determine automatically depending on the name of the resource, or you must specify the model in the BfgResource::$model parameter or override the public static function getResource(): mixed.

Now consider the only scope, where we accept all the parameters sequentially:

Thus, we first get the entire list, and then consistently, and the validation of the required parameters will pass by the PHP means.

The same Rout resource takes various query methods, for such methods there are individual scope We formally alike add the method name in the name of the updatePostScope function - for post

Default scopes

For more convenience, in the set there are already ready-made scope which I can make it easier for you Development.

EloquentAllScopeTrait

Trait to add scope Receiving all records.

EloquentFindScopeTrait

Trait to add scope Search by id.

EloquentFirstScopeTrait

Trait to add scope Gets of the first record.

EloquentForPageScopeTrait

Trait to add scope To get pagination data for the page.

EloquentLatestScopeTrait

Trait to add scope To get the latest row.

EloquentLimitScopeTrait

Trait to add scope To obtain the specified amount of data.

EloquentOrderByScopeTrait

Trait to add scope To obtain assorted data.

EloquentPaginateScopeTrait

Trait to add scope Receive records with pagination.

EloquentRandomScopeTrait

Trait to add scope To obtain random data.

EloquentSkipScopeTrait

Trait to add scope To skip the entries on the ID.

EloquentWhereScopeTrait

Trait to add scope Conditions for request.

EloquentWithScopeTrait

Trait to add scope Loading the resource relationship.

Adding to the download works in depth to each relationship through -.

ModelOnlyScopeTrait

Trait to add scope Field restrictions, it works after sample.

EloquentScopesTrait

General trait for connecting all Eloquent scope.

ModelScopesTrait

General trait for connecting all Model scope.

Final for the query method.

For the GET method, the standard Finish scope All works. For the method of OPTIONS scope first.

Policy

To protect with Laravel Policy, I added you attributes that are responsible for it.

CanResource

Attribute for checking resource.

Will check the id-field-user.

CanScope

Attribute for checking scope.

Since this conditional resource UserResource More will be checked by the politics my-user Or you can specify your own #[CanScope('my-policy')].

CanFields

Attribute for checking field or fields. It only applies to the map parameter.

Will check the id-field-user and name-field-user.

Will check the id-field-user and my-policy.

If the policy field does not fit, it is simply not present in the overall list.

CanUser

Checks the user fields with resource fields.

Will check resource->user_id == auth->user->id.

Or you can manually specify which fields to check.

Will check resource->local_field == auth->user->user_field.

If the policy does not fit, it is simply not present in the overall list, but reserved in the collection.

PHP api

To create one instance:

To create a collection of instances:

Method for autodetect and create instance for collection or single resource:

Inside helpers

PHP Class scope api

To call scope InPHP, you can use the static constructor:

You can get the object for use

Wood support

wood.veskod.com


All versions of resource with dependencies

PHP Build Version
Package Version
Requires php Version >=8.0.0
bfg/embedded-call Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package bfg/resource contains the following files

Loading the files please wait ....