An open API service indexing awesome lists of open source software.

https://github.com/rougin/dextra

"Ready-to-eat" CRUD for PHP frontend.
https://github.com/rougin/dextra

php-crud php-ui ui-template

Last synced: 4 days ago
JSON representation

"Ready-to-eat" CRUD for PHP frontend.

Awesome Lists containing this project

README

          

# Dextra

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]][link-license]
[![Build Status][ico-build]][link-build]
[![Coverage Status][ico-coverage]][link-coverage]
[![Total Downloads][ico-downloads]][link-downloads]

`Dextra` is a PHP utility package that provides templates based on [alpine.js](https://alpinejs.dev/) for handling frontend CRUD.

``` html

<?= $depot = new Depot('items') ?>

<?= $depot->withInit() ?>

<?= $depot->withLoad(10)->setLink('/v1/items') ?>

<?= $depot->withStore()
->addField('name')
->setAlert('Created!', 'Item created.')
->setLink('/v1/items') ?>

```

## Installation

Install the package using [Composer](https://getcomposer.org/):

``` bash
$ composer require rougin/dextra
```

## Basic usage

Use the `Depot` class to initialize the CRUD methods:

``` html
// app/plates/items/depot.php

<?php $depot = new Depot('items') ?>

// ...

```

Then use the available methods below once defined:

### withInit

Creates an `init` method. This method initializes any defined `Select` elements using [tom-select](https://tom-select.js.org/). After initialization, it calls the `load` method with the initial page number to fetch data:

``` html
// app/plates/items/depot.php

// ...

<?= $depot->withInit(1) ?>

```

### withLoad

Creates the `load` method. This method fetches paginated data from a `GET` request. Upon receiving a response, it updates the component's `items` data property with the fetched data and the `limit` data property with the expected items per page (e.g., `10`):

``` html
// app/plates/items/depot.php

// ...

<?= $depot->withLoad(10)
->setLink($url->set('/v1/items')) ?>

```

It also provides configuration for `page` and `limit` keys by using `setPageKey` and `setLimitKey` methods respectively. The default values are `p` for `page` and `l` for `limit`:

``` html
// app/plates/items/depot.php

// ...

<?= $depot->withLoad(10)
->setPageKey('p')
->setLimitKey('l')
->setLink($url->set('/v1/items')) ?>

```

### withStore

Creates a `store` method. This is used for sending a `POST` request to the specified link to create a new item. It collects data from the defined fields, and shows an alert upon successful creation before reloading the data:

``` html
// app/plates/items/depot.php

// ...

<?= $depot->withStore()
->addField('name')
->addField('detail')
->setAlert('Item created!', 'Item successfully created.')
->setLink($url->set('/v1/items')) ?>

```

### withEdit

Creates an `edit` method. This method is used to populate a modal with the data of a selected item. It takes an `item` object as a parameter and assigns its properties to the corresponding fields in the modal. It can also show or hide other modals:

``` html
// app/plates/items/depot.php

// ...

<?= $depot->withEdit()
->addField('name')
->addField('detail')
->addField('id')
->showModal('item-detail-modal') ?>

```

### withUpdate

Creates an `update` method. This method is used for sending a `PUT` request to the specified link to update an existing item. It collects data from the defined fields, includes the item's ID in the request, and shows an alert upon successful update before reloading the data:

``` html
// app/plates/items/depot.php

// ...

<?= $depot->withUpdate()
->addField('name')
->addField('detail')
->setAlert('Item updated!', 'Item successfully updated.')
->setLink($url->set('/v1/items')) ?>

```

### withTrash

Creates a `trash` method. This method is used to populate a modal for confirming the deletion of an item. It takes an `item` object as a parameter and assigns its properties to the corresponding fields in the modal. It can also show or hide other modals:

``` html
// app/plates/items/depot.php

// ...

<?= $depot->withTrash()
->addField('name')
->addField('id')
->showModal('delete-item-modal') ?>

```

### withRemove

Creates a `remove` method. This method is used for sending a `DELETE` request to the specified link to remove an item. It takes the item's ID as a parameter, includes it in the request, and shows an alert upon successful deletion before reloading the data:

``` html
// app/plates/items/depot.php

// ...

<?= $depot->withRemove()
->setAlert('Item deleted!', 'Item successfully deleted.')
->setLink($url->set('/v1/items')) ?>

```

### withClose

Creates a `close` method. This method is used to close modals and reset the values of specified fields. It can also hide other modals and reset fields based on a provided script:

``` html
// app/plates/items/depot.php

// ...

<?= $depot->withClose()
->withScript($script)
->hideModal('delete-item-modal')
->hideModal('item-detail-modal')
->resetField('detail')
->resetField('error')
->resetField('id')
->resetField('name')
->resetField('loadError') ?>

```

The `setDefaults` method can also be used for resetting the data with default values:

``` html
// app/plates/items/depot.php

<?= $script = $form->script('items')
->with('name')
->with('detail')
->with('items', array())
->with('empty', false)
->with('loadError', false)
->with('id', null)
->with('delete', false)
->withError()
->withLoading() ?>

<?= $depot->withClose()
->setDefaults($script->getFields()) ?>

// ...

```

> [!NOTE]
> The `Script` class from [Fortem](https://github.com/rougin/fortem) can be used for resetting the data.

## Changelog

Please see [CHANGELOG][link-changelog] for more recent changes.

## Contributing

See [CONTRIBUTING][link-contributing] on how to contribute to the project.

## License

The MIT License (MIT). Please see [LICENSE][link-license] for more information.

[ico-build]: https://img.shields.io/github/actions/workflow/status/rougin/dextra/build.yml?style=flat-square
[ico-coverage]: https://img.shields.io/codecov/c/github/rougin/dextra?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/rougin/dextra.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-version]: https://img.shields.io/packagist/v/rougin/dextra.svg?style=flat-square

[link-build]: https://github.com/rougin/dextra/actions
[link-changelog]: https://github.com/rougin/dextra/blob/master/CHANGELOG.md
[link-contributing]: https://github.com/rougin/dextra/blob/master/CONTRIBUTING.md
[link-coverage]: https://app.codecov.io/gh/rougin/dextra
[link-downloads]: https://packagist.org/packages/rougin/dextra
[link-license]: https://github.com/rougin/dextra/blob/master/LICENSE.md
[link-packagist]: https://packagist.org/packages/rougin/dextra