open-southeners/laravel-model-permalink 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

open-southeners/laravel-model-permalink

最新稳定版本:1.1.0

Composer 安装命令:

composer require open-southeners/laravel-model-permalink

包简介

Add permalinks to your application's Eloquent models

README 文档

README

Add permalinks to your application's Eloquent models

Getting started

composer require open-southeners/laravel-model-permalink

Usage

First run the command to publish the config and required migrations files:

php artisan vendor:publish --provider="OpenSoutheners\\LaravelModelPermalink\\ServiceProvider"

Then run new migrations:

php artisan migrate

And add the PermalinkAccess interface, HasPermalinks trait and getPermalink method to the models you want to have permalinks:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use OpenSoutheners\LaravelModelPermalink\HasPermalinks;
use OpenSoutheners\LaravelModelPermalink\PermalinkAccess;

class Post extends Model implements PermalinkAccess
{
    use HasPermalinks;

    /**
     * Get permanent link for this model instance.
     */
    public function getPermalink(): string
    {
        // Here is where you return the route that all posts permalinks should use...
        return route('posts.show', $this);
    }
}

Now to generate permalinks to this Post or any configured model you can call the following anywhere in your application's code:

<?php

use App\Models\Post;
use OpenSoutheners\LaravelModelPermalink\GeneratePermalink;

$post = Post::find(1);

GeneratePermalink::for($post);

// or getting directly the route from returned ModelPermalink object

GeneratePermalink::for($post)->getModelPermalink();

Route permissions

The only permalinks route that this package creates is by default accessible to anyone.

This can be configured within gate using the following code on your AppServiceProvider or any other service provider's boot method:

use Illuminate\Database\Eloquent\Model;
use App\Models\User;
use Illuminate\Support\Facades\Gate;

Gate::define('viewModelPermalink', function (?User $user, Model $model) {
    // you can replace this with whatever you like...
    return match (get_class($model)) {
        \App\Models\Post::class => $model->author->is($user),
        \App\Models\User::class => $model->is($user),
        default => false,
    };
});

Partners

skore logo

License

This package is open-sourced software licensed under the MIT license.

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-30