Download the PHP package jobmetric/laravel-url without Composer

On this page you can find all versions of the php package jobmetric/laravel-url. 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 laravel-url

Contributors Forks Stargazers MIT License LinkedIn

Url And Slug for laravel Model

It is a package for url and slug storage management in each model that you can use in your Laravel projects.

Install via composer

Run the following command to pull in the latest version:

Documentation

This package gives each Eloquent model:

One canonical slug stored in a dedicated slugs table (polymorphic one-to-one). A versioned full URL history stored in the urls table (polymorphic one-to-many with soft deletes).

Automatic syncing of URLs when the slug or any parent path segment changes.

Global uniqueness for active full URLs, enforced at the application layer.

Soft-delete/restore support for slugs and URLs, with conflict checks on restore.

Run migrations once after installing the package:

HasUrl Trait

1) Quick Start

1.1 Add the trait and implement UrlContract

HasUrl relies on your model implementing UrlContract (i.e. it must expose how to compute its full URL).

Why UrlContract?

The package must know the current full path for the model. getFullUrl() is your canonical builder for that path.

1.2 Create a record and assign a slug

You don’t set columns on the urls table yourself. You only provide a slug (optional slug_collection) and the package will do the rest.

1.3 Read back the slug and current full URL

2) What the package does for you

3) Slugs & Collections

3.1 One slug, optional collection

Each model gets exactly one slug row. You may tag it with a collection (e.g. to group slugs by context).

If you omit the collection:

3.2 Reading by collection
3.3 Finding models by slug
3.4 Removing the slug

Note: A model without a slug can still compute a full URL if your getFullUrl() does not depend on it, but most setups will.

4) Versioned full URLs

4.1 Automatic syncing

You do not call any URL method on normal saves; syncing happens transparently in the trait:

4.2 Read the current URL or full history
4.3 Resolving owners and redirects
Example redirect middleware (simplified):

5) Cascading URL updates to descendants

If a parent’s path segment changes (e.g., a Category slug), you may need to refresh child URLs (e.g., Products). The trait supports this via an optional method on your model:

When the parent’s slug changes, HasUrl will:

Temporarily disable cascade:

6) Soft delete, restore, and force delete

Examples

7) Rebuilding URLs in bulk

Useful after changing your getFullUrl() logic or migrating data.

8) Exceptions you should know

Example handling

9) API Reference (trait helpers)

Methods below are provided by HasUrl unless otherwise noted.

Slug methods

URL methods

Finders

Bulk operations

Cascade control

10) Real-World Examples

10.1 Category → Product path dependency with cascade

10.2 Changing only the collection
10.3 Handling a user-entered duplicate slug

10.4 301 redirects from old URLs

Combine resolveRedirectTarget() with a middleware (see §4.3). This protects SEO when URLs change over time.

11) Recommended database indexes

These are already baked into the package’s migrations. If you maintain your own schema, consider the following:

12) Events

Use it to update caches, ping search engines, or trigger webhooks.

13) Testing tips


Fallback Route (Smart URL Resolver)

This package can register a single Laravel fallback route that resolves any unmatched path using the versioned urls table:

Enabling / Disabling The fallback is on by default. Control it in config/url.php:

The provider wires it for you:

How it resolves paths

For a request like GET /shop/laptops/mbp-14?color=silver, the controller builds these candidates and looks them up (most recent first):

Then it:

  1. Tries to find an active URL (deleted_at NULL, latest version).
  2. If not found, checks legacy URLs (soft-deleted) and redirects (301) to the canonical URL of the same model (preserving query string).
  3. If still nothing, returns 404 (translated: trans('url::base.exceptions.not_found')).

The UrlMatched Event

When an active Url row is found, the controller emits:

Useful properties:

If no listener sets a response, the controller returns 404.

Writing Listeners (Many Ways)

You can wire listeners in the EventServiceProvider, or use Event::listen at boot time, or register a dedicated invokable class. Below are several patterns with realistic content.

1) Quick inline listener (closure) — Product page

app/Providers/EventServiceProvider.php

app/Http/Controllers/ProductController.php

2) Another closure — Category page with pagination

app/Http/Controllers/CategoryController.php

3) Invokable listener class — clean separation

app/Listeners/HandleMatchedUrl.php

app/Providers/EventServiceProvider.php

4) Listener that does a custom redirect

Use this when you want to override the canonical route entirely.

5) Listener returning an API response (JSON)
6) Using route model binding after match (optional pattern)

You can also “bridge” into a named route:

Security, Middleware & Guards

You can add auth, localization, throttling, etc., by stacking middleware in config/url.php:

If a listener must be protected:

Redirects from Legacy URLs (Built-in)

When a previously active URL becomes obsolete (soft-deleted), the fallback will automatically 301 to the model’s current canonical URL. Query strings are preserved:

You don’t need to configure anything for this behavior; it’s baked into the controller.

Testing Recipes
1) Exactly one event dispatched per request

If you see a 404 in the test, it usually means no listener set a response. Make sure your listener calls $event->respond(...).

2) Legacy redirect
3) JSON response when Accept: application/json
Troubleshooting
Example: Full Setup Summary
  1. Models implement UrlContract and use HasUrl.

  2. Assign slugs (versioned URL is created automatically).

  3. Register listeners to render pages.

  4. Enjoy free 301 redirects for old paths (no extra code).

With this fallback + event pattern, you get one URL entry point that can render anything (products, categories, blogs, CMS pages) based on the database — while preserving SEO via automatic legacy redirects, and keeping your controllers and routes tidy.


Validation: SlugExistRule

SlugExistRule validates that a slug is unique for a given model class and optional collection, ignoring soft-deleted rows and optionally excluding the current record (useful for update forms).

Despite the name, it enforces uniqueness (it fails if a matching active slug already exists). It also normalizes the incoming value exactly like the trait does: Str::slug(trim($value)) and limits it to 100 chars before checking.

Constructor

The rule queries the slugs table with:

If a row exists, validation fails with trans('url::base.rule.exist').

Why use this rule?

Common Recipes

1) Create request: simple uniqueness in a fixed collection

Tip: Set max:100 to align with internal normalization (Str::limit(..., 100)).

2) Update request: exclude current record

If the slug did not change, the rule allows it (because it excludes $productId).

3) Dynamic collection from request (or model type)

If you omit the collection entirely when you later call dispatchSlug(), the trait will fall back to your model’s getSlugCollectionDefault() or its type attribute (see the HasUrl docs).

4) Programmatic validation (no FormRequest)
5) Nested payloads or admin panels

Error Messages

By default, failures use trans('url::base.rule.exist'). You can override per-field:

Or customize the translation key in resources/lang/{locale}/url/base.php:

End-to-End Example

Controller (simplified):

Testing the Rule

1) It fails when another active slug exists
2) It allows reusing a soft-deleted slug
3) It allows keeping the same slug on update

Pitfalls & Tips

With SlugExistRule in place, your forms catch slug collisions before calling dispatchSlug(), keeping user feedback fast and precise while staying perfectly aligned with how the package stores and normalizes slugs.


Contributing

Thank you for considering contributing to the Laravel Url! The contribution guide can be found in the CONTRIBUTING.md.


License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-url with dependencies

PHP Build Version
Package Version
Requires php Version >=8.0.1
ext-json Version *
laravel/framework Version >=9.19
jobmetric/laravel-package-core Version ^1.7
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 jobmetric/laravel-url contains the following files

Loading the files please wait ....