PHPackages                             felixkiss/slug-routes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. felixkiss/slug-routes

AbandonedLibrary[Utility &amp; Helpers](/categories/utility)

felixkiss/slug-routes
=====================

Laravel 4: Slug-aware Route Model Binding

12383[2 issues](https://github.com/felixkiss/slug-routes/issues)[1 PRs](https://github.com/felixkiss/slug-routes/pulls)PHP

Since Aug 30Pushed 12y ago2 watchersCompare

[ Source](https://github.com/felixkiss/slug-routes)[ Packagist](https://packagist.org/packages/felixkiss/slug-routes)[ RSS](/packages/felixkiss-slug-routes/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (1)Used By (0)

What does it do?
================

[](#what-does-it-do)

If you are using the [Route Model Binding](http://laravel.com/docs/routing#route-model-binding) feature in [Laravel 4](http://laravel.com/), slug-routes gives you the ability to use some unique piece of data to represent your records in URLs other than the ID. This can help optimising your URLs for search engines, and generally give your users a better chance to grasp the functionality of your web application.

Installation
============

[](#installation)

Install the package
-------------------

[](#install-the-package)

Install the package through [Composer](http://getcomposer.org/).

In your `composer.json` file:

```
{
    "require": {
        "laravel/framework": "4.0.*",
        "felixkiss/slug-routes": "dev-master"
    }
}
```

Run `composer update` to install the package.

Configure Laravel
-----------------

[](#configure-laravel)

Add the following to your `providers` array in `config/app.php`:

```
'providers' => array(
    // ...

    'Felixkiss\SlugRoutes\SlugRoutesServiceProvider',
),
```

Usage
=====

[](#usage)

*In the following examples, I use `Closure` callback routes only. I use [Controller Routing](http://laravel.com/docs/routing#routing-to-controllers)all the time in my actual apps, but Closures just keep it simple and clean and I think thats more suitable just to showcase the functionality of this package.*

Route Model Binding
-------------------

[](#route-model-binding)

[Route Model Binding](http://laravel.com/docs/routing#route-model-binding) is a really cool feature of Laravel. It gives you the ability to specify a special parameter type for your routes to inject model instances into your Routes. For example, instead of doing the standard thing of

```
// Access via http://example.com/users/1
Route::get('users/{id}', function($id)
{
    $user = User::find($id);

    if(is_null($user))
        App::abort(404);

    return View::make('users.show', compact('user'));
});
```

you can define a route parameter `{user}` that takes an `id` and does the fetching of the record for you. It will even raise an 404, if no user with the existing id exists:

```
// Access via http://example.com/users/1
Route::model('user', 'User');
Route::get('users/{user}', function(User $user)
{
    return View::make('users.show', compact('user'));
});
```

This removes a lot of boilerplate code, since you can reuse this `{user}` in as many of your routes as you want.

The Problem
===========

[](#the-problem)

This is really convenient, but what if you want to have SEO/user-friendly URLs like `/users/taylor-otwell` instead of `/users/1`? Basically, you have three options:

Option 1 - Plain Old Route
--------------------------

[](#option-1---plain-old-route)

```
Route::get('users/{slug}', function($slug)
{
    $user = User::where('slug', $slug);

    if(is_null($user))
        App::abort(404);

    return View::make('users.show', compact('user'));
});
```

This is fine, but we wanted to get rid of the boilerplate. That was the reason we started to use `Route::model()` in the first place.

Option 2 - `Route::bind()`
--------------------------

[](#option-2---routebind)

```
Route::bind('user', function($value, $route)
{
    return User::where('slug', $value)->first();
});
```

[The docs](http://laravel.com/docs/routing#route-model-binding) mention this as the way to go, if we want custom URL to model matching. It works fine but I think the `routes.php` file is not always the right place to define this kind of thing.

Option 3 - Use `slug-routes`
----------------------------

[](#option-3---use-slug-routes)

This package provides `SluggableInterface`, which you can implement in your Model class. If you do, `Route::model('user', 'User')` will recognize it and bind to the database column returned by `getSlugIdentifier()` automatically. If you don't implement the interface it simply falls back to the default behavior provided by `Route::model()`.

Example
=======

[](#example)

Model:

```
