PHPackages                             malyusha/path-history - 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. malyusha/path-history

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

malyusha/path-history
=====================

Laravel package that allows you to create nested/human-readable paths(urls) for your eloquent models from attributes.

v1.1.7(7y ago)41.6kMITPHPPHP ^7.0

Since Mar 5Pushed 7y ago1 watchersCompare

[ Source](https://github.com/malyusha/path-history)[ Packagist](https://packagist.org/packages/malyusha/path-history)[ RSS](/packages/malyusha-path-history/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (6)Versions (13)Used By (0)

 [![](https://camo.githubusercontent.com/fc1943070f06b1762ef335786246ae9fd34536d9f1e3dc1b4b7cd2ff85b11979/68747470733a2f2f7472617669732d63692e636f6d2f6d616c79757368612f706174682d686973746f72792e7376673f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/fc1943070f06b1762ef335786246ae9fd34536d9f1e3dc1b4b7cd2ff85b11979/68747470733a2f2f7472617669732d63692e636f6d2f6d616c79757368612f706174682d686973746f72792e7376673f6272616e63683d6d6173746572)

Path History
============

[](#path-history)

Laravel package that allows you to create nested/human-readable paths(urls) for your eloquent models.

Installation
------------

[](#installation)

run `composer require malyusha/path-history` inside project directory.

Why?
----

[](#why)

Sometimes you may want to create categorized news/articles/products pages and to see final URL to be like `http(s)://your-domain.com/{category}/{product}`. So, what are steps to do routes, resolving such patterns?

- Create routes for each entity. So for each change in base route parameters you will change it's dependencies.
- Create route for category(ies):

```
Route::get('{category_slug}')->name('products_category')->uses('ProductCategoryController@showCategory');
```

- Then create route for product:

```
Route::get('{category_slug}/{product_identifier}')->name('product')->uses('ProductsController@showProduct');
```

But what if you have nested categories for each entity? Ok, we can handle it, just add `where` condition with pattern on routes: `->where('category_slug', '[\w\-/_]+')`. So, now your router will know that `category_slug` can contain slashes, but how can we determine where `category_slug` ends and where `product_identifier` starts? That's it, we need to prefix our product/article route to tell resolver how it works.

Modifying last route:

```
Route::get('{category_slug}/p-{product_identifier}')->name('product')->uses('ProductsController@showProduct');
```

will give us what we wanted.

So what are the **main problems** of this method?

1. Each entity must have it's own route;
2. Can't redirect from old URL to new automatically. Imagine having 2 categories (`black`, `white`) with 20 subcategories each. Each subcategory has ~20-40 products/articles. Now, you decided to change category `black` to `black-color` and you'll have to add redirects from (20 \* ~20-40) URLs to new ones.
3. Prefix for entity with 2-nd level and upper. Not critical but still;

**This package solves problem with that kind of routes and model's URLs**

How it works?
-------------

[](#how-it-works)

1. Package creates table where all URLs from all "slugable" entities will be stored;
2. You add trait to all entities that are "slugable" and determine a little logic for relative entities;
3. You define routes for your system and include package router registrar;
4. Define controller and it's logic for each type of resolved from URL entity;
5. Done!

Getting started
---------------

[](#getting-started)

1. Publish vendors running `vendor:publish --tag=migrations --tag=config`. This will copy configuration file and generate migration file with new table creation.
2. Run `php artisan migrate` to run migration for generated file;
3. For detailed explanation of configuration parameters check out [**configuration**](#configuration) section;

Set up model URL generation
---------------------------

[](#set-up-model-url-generation)

1. Include `\Malyusha\PathHistory\HasPathHistory` trait in your model;

```
