arcticsoftware / polarlinks
简单的超链接管理 CRUD API
Requires
- php: ^8.0.2
Requires (Dev)
- orchestra/testbench: 8.0.x-dev
- phpunit/phpunit: ^9.5.10
This package is auto-updated.
Last update: 2024-09-12 10:04:40 UTC
README
为 Laravel 提供简单的超链接内容 CRUD
PolarLinks 为您的 Laravel 应用程序提供统一的管理网站全局超链接的地方。此包不处理 Laravel 的任何前端组件,但为您的管理面板和所选前端库提供基本的 CRUD API,以便轻松实现。
要求
- Laravel >= 8.*
- PHP >= 8.*
安装
您可以通过 composer 安装此包
composer require arcticsoftware/polarlinks
安装完包后,在进行数据库迁移之前,您可能需要自定义和定义包将要使用的表名。该包定义并需要两个数据库表。一个用于链接集合,一个用于链接本身。默认情况下,这些是
- polarsections
- polarlinks
分别。要在安装后发布配置文件,请在您的 Laravel 安装根目录中运行以下命令
php artisan vendor:publish --provider="ArcticSoftware\PolarLinks\PolarLinksServiceProvider" --tag="config"
从现在开始,您可以在 [laravel_root]/config/polarlinks.php 中编辑已发布的配置文件
一旦您对您的选择满意,请通过在您的 Laravel 安装根目录中运行以下命令来发布迁移
php artisan vendor:publish --provider="ArcticSoftware\PolarLinks\PolarLinksServiceProvider" --tag="migrations"
然后运行 php artisan migrate 以运行数据库迁移。现在包已准备好使用。
用法
通过一些定义好的门面访问底层模型和数据
PolarLink
PolarLink 类提供一系列静态函数来操作链接数据。对这个类的每一次调用都依赖于一个名称标识符,因此无论您要运行什么操作,您都必须提供名称函数
PolarLink::name('a_name')
名称标识符必须对表是唯一的,并且只能包含字母数字字符和下划线。从现在开始,您可以在模型上定义可选属性来填充链接记录
PolarLink::name('a_name') ->title('A link title') // String field ->weight(3) // Int field ->url('/site/link') // String field that accepts valid URLs and paths ->description('A description') // Text field ->published(false) // A boolean field ->create(); // Creates the link with provided data
类上的最后一个可调用函数始终是一个操作。PolarLink::class 可用的函数操作有
function get() : Link // Returns a single eloquent link model, or *null* if the link was not found in the database table, using the provided name. function create() : Link // Inserts a new link with the provided name as an identifier and any optional data. Returns a single eloquent link model after creation. function update() : Link // Updates a link with the provided name as an identifier and any optional data. Returns a single eloquent link model after updating or a *null* value if no model was found in the database with the provided name. function delete() : void // Deletes a link with the provided name as an identifier. function newName(string $newName) : Link // Renames the link with a new identifier, returns Link eloquent model if successful or *null* if no link where found
PolarLink::class 还有以下静态实用函数(不要依赖于使用静态名称函数)
function checkifExists(string $linkName) : bool // Check if the link with the provided name exists in the database. Returns true or false depending on the result function testUrl(string $url) : bool // Check if the string passed validates as a URL or relative internal site path. Returns true or false depending on the result function testName(string $name) : bool // Check if the string passed validates as a Link identifier. Returns true or false depending on the result
PolarSection
PolarSection 类提供对链接进行分类和排序的手段,它定义为 LinkSection-Link eloquent 模型之间的一对多关系。除了常规的 CRUD 程序外,它还允许您以 Eloquent\Collection、PHP 数组或 JSON 字符串的形式检索链接集合。
function get() : LinkSection
使用提供的名称返回单个 eloquent linksection 模型,如果数据库表中未找到 linksection,则返回 null。
如果 linksection 有一个或多个子链接模型,您可以通过使用 load() 函数并将选项作为数组传递来检索这些子链接,如下所示
LinkSection::name('mainmenu_links') ->load([ 'format' => 'json' ]) ->get();
这将返回与该部分相关联的所有链接(及其数据)的 JSON 数组字符串。格式参数接受 3 个选项
- 'collection' - 返回 Eloquent\Collection 的链接
- 'array' - 返回 PHP 数组中的链接
- 'json' - 返回 JSON 数组字符串中的链接
function create() : LinkSection // Inserts a new linksection with the provided name as an identifier. Returns a single eloquent linksection model after creation. function delete(bool $purgeLinks = false) : void // Deletes a linksection with the provided name as an identifier. If the passed *$purgeLinks* parameter is true, it will also delete any associated links. If the parameter is false (default), it will only dissassociate the model relationship and leave the links associated with the section intact in the database. function attach(Link $polarLink) : void // Attaches a Link model to the Section, with the provided name. function attachMore(Collection $polarLinkEloquentCollection) : void // Attaches an Eloquent collection of Link models with the provided Section name. function empty() : void // Dissassociates all links in the section function purge() : void // Deletes all links in the section function newName(string $newNameIdentifier) : void // Renames the linksection with a new identifier, given the old name.
PolarSection::class 还有以下静态实用函数(不要依赖于使用静态名称函数)
checkifExists(string $linkSectionName) : bool // Check if the linksection with the provided parameter name exists in the database. Returns true or false depending on the result
作者绑定
Link 和 LinkSection 模型都可以与您的用户(或任何其他模型)建立多态关系,以定义数据的所有权。这可以通过给您的模型添加必要的特征来实现,以便与这两个模型中的任意一个或两个相关联。
在 User 模型中的示例实现
namespace App\Models; use ArcticSoftware\PolarLinks\Traits\HasLinks; use ArcticSoftware\PolarLinks\Traits\HasLinkSections; class User extends Authenticatable implements MustVerifyEmail { use HasLinks, HasLinkSections; }
现在可以通过 eloquent 访问这种关系
$author->links();
或
$author->linkSections();
分别
测试
composer test
变更日志
请参阅变更日志以了解不同版本之间的更改详情。
贡献
如果您想对我们的代码做出贡献,请联系我们,我们将为您设置所需的分支和权限以便提交更改。我们一定会在此页(以及我们的公司页面上)给予您应有的认可(如果您的贡献意义重大)。
安全性
如果您在代码中发现任何安全漏洞,请直接联系我们,而不是使用问题跟踪器。
许可证
本软件根据 MIT 许可证条款授权。请查阅附带的license.md文件以了解详情。