Download the PHP package glaivepro/ajaxable without Composer

On this page you can find all versions of the php package glaivepro/ajaxable. 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 ajaxable

Ajaxable

Ajaxable is a Laravel package that allows you to control (create, edit, delete) Eloquent models without bothering you on the backend. Or even frontend. Add markup to your html or use helpers and it works!

See docs at https://glaivepro.github.io/Ajaxable

Getting started - installation, setup and examples

Create ajaxable input in your view, add the CSRF token and the javascript library (this one depends on jQuery):

Now you have a field that saves any changes to database (if the user is authenticated).

If you need it a bit more custom, you can create the HTML controls yourself. For example this is how you delete App\Tag with the specified ID and remove the row (this is why it's wrapped in .ajaxable-row):

Of course, you are not obliged to use the provided javascript. Here's how you could create an article with title "Test article 4" using plain javascript:

Usage in details

Preliminaries

Make your model ajaxable.

In case you want fields to update models (i.e. you are not working with HTTP interface directly), include the CSRF token and JS library (it depends on jQuery) in your views.

Generating HTML

Editing models

To obtain a field that synces changes to database, do this in your Blade template:

Some options can be supplied as well

List of supported options:

Option Description
type select or textarea will make the respective field, anything else sets the type attribute on input element
classes adds classes (string or array) to element
attributes adds attributes (array of 'attribute' => 'value'); doesn't work for type or class
options array of arrays (containing value and text keys) or strings, for select elements

Deleting models

To obtain a button that invokes deletion of element once clicked, use the deleteButton method and supply the content of the element.

You can specify some options:

List of supported options:

Option Description
tag HTML tag
classes adds classes (string or array) to element
attributes adds attributes (array of 'attribute' => 'value'); doesn't work for class

If you need button (and maybe something else) removed on successful delete, wrap it (including the button) in .ajaxable-row.

Creating models

To create models, use the static creatorButton() method.

Set some initial attribute values

Add the fields so user could set some values before creation as well

If you need multiple creators with fields for same model on the same page, supply an ID to distinguish the sets.

In addition creatorField supports all the same methods that editor does and creatorButton supports all the options that deleteButton does.

Writing your own HTML

In many cases you'll want to write your own html but still want it to work with the included javascript library. You'll just have to add a little markup.

We'll start with editing as that's the simplest.

Editing model attributes

Specify the model and add the ajaxable-edit class to your field.

In case of a validation error user will get an alert. If you want to display it on html instead, wrap the input in .form-group. Error will be added in span.error-block.help-block and class has-error will be added to .form-group.

Deleting models

A click on .ajaxable-delete will remove an item specified by data-* attributes. HTML will also get removed if you wrap it (including the button) in .ajaxable-row.

Creating new models

A click on .ajaxable-creator will make a request to create an object. Specify model in data-model attribute.

You may also supply any additional data-attribute_* attributes to be set on the new model.

User supplied values for new models

You can have an input that sets (on change) the value on creator:

In case of validation error user will get alerts. If you want to display alerts neatly instead, wrap input in .form-group:

This .form-group will then get class has-error added and span.error-block.help-block appended with the message.

Adding the created model to a list

Often times you have a list of entries and need the new item appended to the list. We can do this if your models return html.

Creator should have the list specified in data-ajaxable-list.

The above example would append created html (using ajaxable.tag view) to #tag-list and add class ajaxable-highlight for 1.5 seconds (for highlighting purposes). If you want to prepend instead, specify data-ajaxable-list-position="first". If you want the new item scrolled into view, specify data-ajaxable-scroll="true".

Using HTTP interface directly

Use your own JavaScript (or whatever else) to invoke stuff happening on your models. Here's what we provide:

Route Required parameters Optional parameters Response
ajaxable.create model Initial values (key:value pairs in attributes) Model in JSON, optional HMTL.
ajaxable.update model, id, key, value Model in JSON, optional HTML
ajaxable.delete model, id Confirmation only
ajaxable.updateOrCreate model Constraints (key:value pairs in wheres) and values (key:value pairs in attributes) Model in JSON, optional HTML
ajaxable.control model, id, action parameters - supply whatever to be passed to called action. Whatever you decide to return
ajaxable.addMedia model, id, media collection, name Media object and URL
ajaxable.deleteMedia model, id, media_id Confirmation only

Example. To update title to New Title on App\Article with ID 155 you'd POST {model: 'App\Article', id: 155, key: 'title', value: 'New Title'} to {{route(ajaxable.update)}}.

The ajaxable.control route could in theory call any method on your model. In practice the included checks refuse most actions and you should explicitly list what you want to allow in the model. Use sparingly!

The above routes will only work for POST requests and you must include XSRF token so Laravel would let you through.

Here are some get routes to retrieve data:

Route Required parameters Optional parameters Response
ajaxable.retrieve model, id Confirmation, model in JSON, optional HTML
ajaxable.list model wheres as key:value pairs, scopes as scopeName or scopeName:parameter1,param2 Model in JSON, optional HTML
ajaxable.getMedia model, id collection Media object and URL

Note. The media routes will only work if your model uses Laravel Medialibrary.

Returning HTML

Some requests (create, update, updateOrCreate, retrieve) can include rendered HTML in the response if you specify view: true on the request.

To return rendered html you should create a view. By default 'ajaxable.'.camel_case(class_basename($model)) will be looked for. For example, App\Tag and App\Stats\UserFault will look for ajaxable.tag and ajaxable.userFault respectively so you should create tag.blade.php and userFault.blade.php inside resources/views/ajaxable.

This can be overriden by specifying $rowView property on the model or supplying viewname in the request.

Camel of basename will be passed to view. For example $tags or $userFault.

The list method is able to return HTML as well if you specify view: true. The response will be rendered through template selected in this order:

Authorization and validation

By default the actions are allowed to all authenticated users. This is only appropriate when there is just a single class of users. In most cases you'd override the allowAjaxableTo method on your model

Here's an example that one might use for the contact form messages

Customizing responses

By default a successful request is responded with a JSON array that contains "success": 1. Something else like object, collection, view could also be set for the appropriate actions.

However, all of this can be overriden. The requests go through a respondAfter method on the model that will call try to call a specific method like respondAfterDelete, respondAfterCreate, respondAfterRetrieve etc, the last word corresponding to route name or the action value in case of ajaxable.control route. If the specific method is not found, fallbackResponse will be called which does some sensible defaults.

If you want to customize responses after retrieval, you overwrite the corresponding method on your model:

You may override all of the respondAfter method if you feel the need.

Tips & Tricks

This section tells you how to do some stuff that the package doesn't explicitly implement.

Value should be transformed before saving or retrieving

Use the Laravels mechanic of getters and setters (accessors).

I must do something else when action is happening.

Use Laravel Events. If you need to distinguish ajaxable events from other, check request().

And remember that you can add small handlers directly in the model. For example, refuse deleting if the model is used and clean up otherwise:

Field name is uncertain or it's json or it's complicated...

Usually this is a sign that you should ride on your own. This library makes the simple stuff simpler. If it's complicated, the overhead of making your own route and controller will be miniscule.

However, if you insist that you really want to use this and it's only this one time... well, there are a few paths to hack around.

You can do whatever you want in the allowAjaxableTo method before you allow ajaxable to work further.

TODO, goals and possible goals

Road to v1

Random frontend ideas, needs general reconsideration

Random other ideas

Change log

0.10 is a major rewrite of library. We intend to have less changes (especially less breaking changes) in the future and we are aiming to make this library production-ready.

Please see CHANGELOG for more information on what has changed recently.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.


All versions of ajaxable with dependencies

PHP Build Version
Package Version
Requires illuminate/support Version >=5.5
illuminate/database Version >=5.5
php Version >=7.1
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 glaivepro/ajaxable contains the following files

Loading the files please wait ....