承接 simtecsystem/cakephp-sluggable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

simtecsystem/cakephp-sluggable

Composer 安装命令:

composer require simtecsystem/cakephp-sluggable

包简介

Plugin for CakePHP 3.x that enables automatic, configurable slugging of database fields

README 文档

README

Plugin for CakePHP 3.x that enables automatic, configurable slugging of database fields

WHY?

Because slugs are great for human-readable yet seo-friendly page titles, urls, image urls, etc! They're pretty much the standard nowadays and CakePHP makes it super easy to give your app the power to create them for you.

HOW?

Just add the Sluggable.Sluggable behaviour to any model whose field(s) you need to slug. See the usage section for customization.

Requirements

TOC

  1. Plugin Installation
  2. Usage
  3. Examples
  4. Contributing

Plugin Installation

  1. Composer Install
  2. Manual Install
  3. Loading the plugin in your app
  4. Setting up the namespace / autoloader

Composer Install

This plugin is on Packagist which means it can be easily installed with Composer.

composer require simtecsystem/cakephp-sluggable

Then simply load the plugin normally in your config/bootstrap.php file

# in ../config/bootstrap.php - right after Plugin::load('Migrations') is fine!
Plugin::load('Sluggable');

Manual Install

You can also manually load this plugin in your App

loading the plugin in your app

Add the source code in this project into plugins/Sluggable

Then configure your App to actually load this plugin

# in ../config/bootstrap.php
Plugin::load('Sluggable');
setting up the namespace / autoloader

Tell the autoloader where to find your namespace in your composer.json file

	(..)
    "autoload": {
        "psr-4": {
           (..)
            "Sluggable\\": "./plugins/Sluggable/src"
        }
    },
    (..)

Then you need to issue the following command on the commandline

	php composer.phar dumpautoload

If you are unable to get composer autoloading to work, add 'autoload' => true line in your bootstrap.php Plugin::load(..) command (see loading section)

Slug Behavior

The sluggable behavior is extremely easy to implement, simply add it, like any other behavior, to your Table

class PostsTable extends Table
{
	public function initialize(array $options)
	{
		parent::initialize($options);
		
		$this->addBehavior('Sluggable.Sluggable');
	}
}

By default the plugin will automatically generate a slug based on name, will store it in a column called slug and will use a dash - replacement, and will NOT automatically overwrite the slug field whenever name changes.

All of these settings are, of course, configurable.

  • pattern
    • :name (default)
    • a \Cake\Utility\Text::insert()-friendly tokenized string. any of the entity fields are valid options
  • field
    • slug (default)
    • field in the entity to store the slug
  • replacement
    • - (default)
    • string used to replace spaces when building the slug
  • overwrite
    • false (default)
    • true, if the slug should ALWAYS be re-generated on save. false, to generate once

Examples

Generate a slug based on the title field instead of name

class PostsTable extends Table
{
	public function initialize(array $options)
	{
		parent::initialize($options);
		
		$this->addBehavior('Sluggable.Sluggable', [
			'pattern' => ':title',
		]);
	}
}

Generate a slug based on id and title

class PostsTable extends Table
{
	public function initialize(array $options)
	{
		parent::initialize($options);
		
		$this->addBehavior('Sluggable.Sluggable', [
			'pattern' => ':id-:title',
		]);
	}
}

Generate a slug based on the latest version of the title (always)

class PostsTable extends Table
{
	public function initialize(array $options)
	{
		parent::initialize($options);
		
		$this->addBehavior('Sluggable.Sluggable', [
			'pattern' => ':title',
			'overwrite' => true,
		]);
	}
}

Generate a slug normally, but store it in the foo column

class PostsTable extends Table
{
	public function initialize(array $options)
	{
		parent::initialize($options);
		
		$this->addBehavior('Sluggable.Sluggable', [
			'field' => 'foo',
		]);
	}
}

Generate a slug using . dots instead of - dashes

class PostsTable extends Table
{
	public function initialize(array $options)
	{
		parent::initialize($options);
		
		$this->addBehavior('Sluggable.Sluggable', [
			'replacement' => '.',
		]);
	}
}

Slug Utility

The Sluggable Plugin adds a Utility class Slug that can be called statically. This is the function used by the Behavior to actually generate the slug.

It is capable of handling a string, array, or entity in conjunction with a simple string or Text::insert-friendly pattern.

To use the Utility, simply add the following to your class header

	use Sluggable\Utility\Slug;

The Utility provides the following function

 /**
     * Turns a string (and optionally a dynamic, data-injected string) into a slugged value
     * @param $pattern string a simple string (e.g. 'slug me') 
     * 						  or Text::insert-friendly string (e.g. ':id-:name')
     * @param $data mixed an Array or Entity of data to Text::insert inject into $pattern
     * @param $replacement string the character to replace non-slug-friendly characters with (default '-')
     * @return string the slugged string
     */
    Slug::generate($pattern, $data = [], $replacement = '-');

Examples

	use Sluggable\Utility\Slug;

	echo Slug::generate('slug me');
	# 'slug-me'

	echo Slug::generate('SLUG(!@#(ME');
    # 'slug-me'

    echo Slug::generate('a really long slug that i just made');
    # 'a-really-long-slug-that-i-just-made'

To Text::insert via an array...

	$data = [
		'id' => 123,
		'name' => 'abc',
		'description' => 'Hello, World!',
	];

	$slug = Slug::generate(':id-:name', $data);
	# '123-abc'

	$slug = Slug::generate(':description', $data);
	# 'hello-world'

To Text::insert via Entity properties...

	$data = new Entity([
		'id' => 123,
		'name' => 'abc',
		'description' => 'Hello, World!',
	]);

	$slug = Slug::generate(':id-:name', $data);
	# '123-abc'

	$slug = Slug::generate(':description', $data);
	# 'hello-world'

Contributing

If you'd like to contribute, please submit a PR with your changes!

Requests will be accepted more readily if they come complete with TESTS :D

simtecsystem/cakephp-sluggable 适用场景与选型建议

simtecsystem/cakephp-sluggable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 03 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 simtecsystem/cakephp-sluggable 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-03-01