定制 zeynallow/laravel-hashtags 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

zeynallow/laravel-hashtags

Composer 安装命令:

composer require zeynallow/laravel-hashtags

包简介

Laravel package for hashtag and mention analysis, management and rendering.

README 文档

README

Laravel package for hashtag and mention analysis, management and rendering. This package is used to extract #hashtags and @mentions from any text field, store them in the database and convert them to HTML links.

🚀 Features

  • ✅ Hashtag and mention extraction (#Laravel, @zeynallow)
  • ✅ Morphable relationship support (taggables)
  • ✅ Blade helpers and directives
  • ✅ Secure HTML rendering
  • ✅ Trending hashtags
  • ✅ Search functionality
  • ✅ Cache support
  • ✅ Eloquent scopes
  • ✅ Service layer architecture
  • ✅ Configuration options

📦 Installation

Add via Composer

composer require zeynallow/laravel-hashtags

Configuration and migration

# Publish config file
php artisan vendor:publish --tag=laravel-hashtags-config

# Publish migrations
php artisan vendor:publish --tag=laravel-hashtags-migrations

# Run migrations
php artisan migrate

🧠 Usage

1. Add Trait to Your Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Zeynallow\Hashtags\Traits\HasTags;

class Post extends Model
{
    use HasTags;

    protected $fillable = ['title', 'body'];
}

2. Automatically attach hashtags

$post = Post::create([
    'title' => 'About Laravel',
    'body' => 'This post is about #Laravel and #PHP. Thank you @zeynallow!'
]);

// Automatically attach hashtags
$post->attachHashtags();

// Now hashtags are stored in database and linked to the post
$post->hashtags; // Returns Hashtag model collection

3. Extract hashtags programmatically

use Zeynallow\Hashtags\HashtagExtractor;

$extractor = new HashtagExtractor();

$parsed = $extractor->parse("Testing #laravel @zeynal #php");

// Result:
[
    'hashtags' => ['laravel', 'php'],
    'mentions' => ['zeynal'],
]

4. Render in Blade with links

{{-- Simple usage --}}
{!! tagify($post->body) !!}

{{-- Without links --}}
{!! tagify($post->body, false) !!}

{{-- Using Blade directives --}}
@hashtags($post->body)
@hashtagLinks($post->body)

5. Helper functions

// Extract hashtags
$hashtags = extract_hashtags($text);

// Extract mentions
$mentions = extract_mentions($text);

// Check if text has hashtags
if (has_hashtags($text)) {
    // Has hashtags
}

// Check if text has mentions
if (has_mentions($text)) {
    // Has mentions
}

🔧 API Documentation

HashtagExtractor

use Zeynallow\Hashtags\HashtagExtractor;

$extractor = new HashtagExtractor();

// Main parse method
$result = $extractor->parse($text);

// Extract only hashtags
$hashtags = $extractor->extractHashtags($text);

// Extract only mentions
$mentions = $extractor->extractMentions($text);

// Check if text has hashtags
$hasHashtags = $extractor->hasHashtags($text);

// Check if text has mentions
$hasMentions = $extractor->hasMentions($text);

HashtagService

use Zeynallow\Hashtags\Services\HashtagService;

$service = app(HashtagService::class);

// Attach hashtags to model
$service->attachToModel($post, 'body');

// Get trending hashtags
$trending = $service->getTrending(10);

// Search hashtags
$results = $service->search('laravel', 5);

HasTags Trait

// Attach hashtags to model
$post->attachHashtags('body');

// Remove all hashtags
$post->detachAllHashtags();

// Sync hashtags (remove old ones, add new ones)
$post->syncHashtags('body');

// Check if model has specific hashtag
if ($post->hasHashtag('laravel')) {
    // Has Laravel hashtag
}

// Get hashtag names
$names = $post->getHashtagNames(); // ['laravel', 'php']

// Get hashtag display names
$displayNames = $post->getHashtagDisplayNames(); // ['#laravel', '#php']

Eloquent Scopes

// Find posts with specific hashtag
$posts = Post::withHashtag('laravel')->get();

// Find posts with any of the given hashtags
$posts = Post::withAnyHashtag(['laravel', 'php'])->get();

// Find posts with all of the given hashtags
$posts = Post::withAllHashtags(['laravel', 'php'])->get();

⚙️ Configuration

You can configure the following options in config/laravel-hashtags.php:

return [
    // Regex patterns
    'hashtag_pattern' => '/#(\p{L}[\p{L}0-9-_]*)/u',
    'mention_pattern' => '/@(\p{L}[\p{L}0-9-_]*)/u',
    
    // URL prefixes
    'hashtag_url_prefix' => '/tags/',
    'mention_url_prefix' => '/users/',
    
    // CSS classes
    'hashtag_css_class' => 'hashtag-link',
    'mention_css_class' => 'mention-link',
    
    // Auto linking
    'enable_tagify_links' => true,
    
    // Taggable models
    'taggable_models' => [
        'App\Models\Post',
        'App\Models\Comment',
    ],
    
    // Auto attach
    'auto_attach_on_save' => false,
    'auto_attach_text_field' => 'body',
    
    // Cache
    'cache_trending' => true,
    'trending_cache_ttl' => 3600,
];

🎨 CSS Styling

CSS for hashtag and mention links:

.hashtag-link {
    color: #1da1f2;
    text-decoration: none;
    font-weight: 500;
}

.hashtag-link:hover {
    text-decoration: underline;
}

.mention-link {
    color: #1da1f2;
    text-decoration: none;
    font-weight: 500;
}

.mention-link:hover {
    text-decoration: underline;
}

🧪 Testing

# Run tests
composer test

📝 License

This package is released under the MIT license. See LICENSE file for details.

🤝 Contributing

To contribute:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Create a Pull Request

📞 Contact

🔄 Updates

v1.0.0

  • Initial release with full feature set
  • Service layer architecture
  • Better error handling
  • Cache support
  • Blade directives
  • More helper functions
  • Performance improvements

zeynallow/laravel-hashtags 适用场景与选型建议

zeynallow/laravel-hashtags 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 56 次下载、GitHub Stars 达 4, 最近一次更新时间为 2025 年 06 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「eloquent」 「social-media」 「mentions」 「text-processing」 「hashtags」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 zeynallow/laravel-hashtags 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 zeynallow/laravel-hashtags 我们能提供哪些服务?
定制开发 / 二次开发

基于 zeynallow/laravel-hashtags 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-22