PHP code example of cwbit / cakephp-sluggable

1. Go to this page and download the library: Download cwbit/cakephp-sluggable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cwbit / cakephp-sluggable example snippets


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

# in ../config/bootstrap.php
Plugin::load('Sluggable');


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


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


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


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


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


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

 /**
     * 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 = '-');


	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'

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

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

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


	$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'

	php composer.phar dumpautoload