Download the PHP package jeremyharris/slugger without Composer

On this page you can find all versions of the php package jeremyharris/slugger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package slugger

build
status

Slugger

BREAKING CHANGE The original Slugger used named parameters exclusively. It now defaults to passed arguments. The configuration array has also changed. Please read the README for information on the new configuration. To use the original version, checkout the version 1.0 tag.

Slugger is a plugin that basically rewrites cake urls (using routing) into slugged urls automatically:

'/posts/view/12'

automatically becomes

'/posts/view/my-post-title'

This avoids the need to store a slug in the db, manage it, check for duplicates, etc. It also avoids the Model::findBySlug() solution that many people use. Search for your post using the primary key instead! (Initial development sparked by Mark Story's blog).

The slug is then transparently reverted back into the proper format for your controller action.

Requirements

Installation

Manual

GIT Submodule

In your app directory type:

git submodule add git://github.com/jeremyharris/slugger.git Plugin/Slugger
git submodule update --init

Composer

Ensure require is present in composer.json. This will install the plugin into Plugin/Slugger:

{
    "require": {
        "jeremyharris/slugger": "dev-master"
    }
}

If you need the old version of slugger, make sure to replace "dev-master" with "1.0".

Usage

App::uses('SluggableRoute', 'Slugger.Routing/Route');

Router::connect(/posts/:action/:Post,
    array(),
    array(
        'routeClass' => 'Slugger.SluggableRoute',
        'models' => array('Post')
    )
);

This is the minimal default configuration. We're using the SluggableRoute class for this route, and checking for the Post model to generate slugs. The :Post key is our passed key ($id in the action).

Options

Router::connect(/posts/:action/:Post,
    array(),
    array(
        'routeClass' => 'Slugger.SluggableRoute',
        'models' => array(
            '<MODEL_NAME>' => array(
                'slugField' => '<FIELD_TO_SLUG>',
                'param' => '<SLUG_PARAM>'
            )
        ),
        'slugFunction' => '<SLUG_FUNCTION>'
    )
);

Defining the slug param

The slug parameter is what Slugger pulls from the routed URL and replaces with your slug. By default, it checks for the :<MODEL_NAME> passed key.

:Post Slugs the :Post route element:

/post/view/5 --> /post/view/my-post-title

Post Slugs the Post named param:

/post/view/Post:5 --> /post/view/my-post-title

Custom slug function

You can define a custom function to use when slugging your urls by setting the 'slugFunction' key in the route options. This key accepts a php callback and passes one argument, the string to slug. It expects a string to be returned.

For example, to use a custom function:

function my_custom_iconv_slugger($str) {
    $str = preg_replace('/[^a-z0-9 ]/i', '', iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $str));
    $quotedReplacement = preg_quote($replacement, '/');
    $merge = array(
        '/[^\s\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
        '/\\s+/' => $replacement,
        sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
    );
    return strtolower(preg_replace(array_keys($merge), array_values($merge), $str));
}
Router::connect('/posts/:action/*',
    array(),
    array(
        'routeClass' => 'SluggableRoute',
        'models' => array('Post'),
        'slugFunction' => 'my_custom_iconv_slugger'
    )
);

iconv is a PHP module that encodes strings in a different character set, thereby stripping invalid characters. It's much faster but depends on your system's setup.

Using Slugger in your application

Create links using Cake's helpers and Router to take advantage of automatically generated slugs:

array(
    'controller' => 'posts',
    'action' => 'view',
    12
)

turns into a url string like /posts/view/my-post-title, then back into the proper request for your controller to handle by putting 12 back into the passed arguments. In your controller, get the post id by checking:

function view($id = null) {
    $post = $this->Post->read(null, $id);
    // do controller stuff
}

If you have defined a custom <SLUG_PARAM>, Slugger will replace whatever parameter type you chose and put the original route array back together.

Caching

Slugger caches by default. When you update records that the Sluggable route uses, you'll need to remove the cache. For example, updating a User's username

App::uses('SlugCache', 'Slugger.Cache');

$this->User->id = 3;
$this->User->saveField('username', 'newUsername');
// invalidate user slugs
SlugCache::invalidate('User');

To invalidate a single user you must regenerate the slug yourself and save it in the cache:

$this->User->id = 3;
$newSlug = 'my-new-slug';
$userSlugs = SlugCache::get('User');
$userSlugs[$this->User->id] = $newSlug;
SlugCache::set('User', $userSlugs);

Slugger uses the 'Slugger' cache config to cache, so customize that configuration to change caching engines.

Examples

Passed Argument example using first arg (default bake)

Router::connect(/posts/:action/*,
    array(),
    array(
        'routeClass' => 'Slugger.SluggableRoute',
        'models' => array('Post')
    )
);

Using the above route

array(
    'controller' => 'posts',
    'action' => 'view',
    12
)

Becomes the /posts/view/sluggable-is-cool, and is accessed in the controller as so:

function view($id) {
    $post = $this->Post->read(null, $id);
    // do controller stuff
}

Passed Argument example using keyed passed arg

Router::connect(/posts/:action/:post_id/*,
    array(),
    array(
        'pass' => array('post_id'),
        'routeClass' => 'Slugger.SluggableRoute',
        'models' => array(
            'Post' => array(
                'param' => ':post_id'
            )
        )
    )
);

Using the above route

array(
    'controller' => 'posts',
    'action' => 'view',
    'anotherArg',
    'post_id' => 12
)

Becomes the /posts/view/anotherArg/sluggable-is-cool, and is accessed in the controller as so:

function view($id, $anotherArgWillBeHere) {
    $post = $this->Post->read(null, $id);
    // do controller stuff
}

Caveats

A couple of things to note if using keyed passed args in your routes.

Named Parameter example

Router::connect(/posts/:action/*,
    array(),
    array(
        'routeClass' => 'Slugger.SluggableRoute',
        'models' => array(
            'Post' => array(
                'slugField' => 'post_title',
                'param' => 'Post'
            ),
            'Author' => array(
                'param' => 'Author'
            )
        )
    )
);

Using the above route

array(
    'controller' => 'posts',
    'action' => 'view',
    'Post' => 12,
    'Author' => 1
)

Becomes the /posts/view/jeremy/sluggable-is-cool, and is accessed in the controller as so:

function view() {
    $post = $this->Post->read(null, $this->request->params['named']['Post']);
    $author = $this->Post->Author->read(null, $this->request->params['named']['Author']);
    // do controller stuff
}

Notes and Features

Limitations

License

Licensed under The MIT License http://www.opensource.org/licenses/mit-license.php Redistributions of files must retain the above copyright notice.


All versions of slugger with dependencies

PHP Build Version
Package Version
Requires composer/installers Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package jeremyharris/slugger contains the following files

Loading the files please wait ...