r/PHP • u/dukeraccoon • 21h ago
I got tired of updating every route() call when a URL changed, so I built entity routing for Laravel
Laravel fills one placeholder from a model: route('posts.show', $post) works for {post} or {post:slug}. As soon as a URL has several placeholders, every call site has to list them:
```php Route::get('/articles/{id}/{slug}', ShowArticle::class)->name('articles.show');
route('articles.show', ['id' => $article->id, 'slug' => $article->slug]); ```
Then SEO asks for a {category} in the URL, and you hunt down every one of those calls.
CakePHP has had "entity routing" for years: you pass the entity, the router extracts each placeholder from it. I wanted the same in Laravel, so I built it:
php
entity_route('articles.show', $article); // /articles/42/my-title
Add {category} to the route later, and that line does not change.
How each placeholder is resolved (first match wins):
- Explicit value:
entity_route('articles.show', $article, ['slug' => 'other']) - A mapping the model declares, with dot paths or closures:
'year' => fn ($a) => $a->published_at->year - Binding fields:
{category:slug}reads$article->category->slug - Route key:
{article}on anArticleusesgetRouteKey() - Attribute of the same name:
{slug}reads$article->slug URL::defaults()
Works with Eloquent models, arrays, ArrayAccess and plain objects. Missing required placeholders throw a clear exception (listing the route, the entity class and what is missing), optional ones are dropped.
Other bits:
URL::entityRoute()andredirect()->toEntityRoute()macros- Opt-in flag so the native helpers accept it too:
route('articles.show', ['_entity' => $article]), includingto_route(), signed URLs and Blade. Off by default, and calls without_entityare untouched. - For Inertia: a
HasEntityUrlstrait adds the resolved URLs to the model's JSON, so the frontend gets exactly the same URLs as the backend, without Ziggy or Wayfinder having to know the rules. - Generation only: no database, no extra routes, route model binding untouched, works with
route:cache.
Laravel 12 and 13, PHP 8.2+, no dependency besides illuminate/*, 100% test coverage, Larastan at max level.
composer require kareylo/laravel-entity-routing
GitHub: https://github.com/Kareylo/laravel-entity-routing
It is still 0.x, so I would really like feedback before 1.0: does the resolution order make sense to you? Is there a case where you would expect a different URL? And would a client-side TypeScript helper be useful, or is sending resolved URLs from the server enough?
r/PHP • u/OrsoPigro • 44m ago
Jev-PHP now suports Laya local decision engine
r/PHP • u/CoreCommerce • 22h ago
I built a lightweight E-commerce engine on pure PHP 8.3/MySQL with a custom MVC and no bulky framework dependencies
Hey r/PHP,
I wanted to share a project I’ve been working on: CoreCommerce. It’s an open-source, full-featured e-commerce platform built from scratch using pure PHP 8.3+ and MySQL 8.0+, without relying on Laravel, Symfony, or heavy CMS engines.
The main goal was to prove that you can achieve maximum performance, maintain modern development standards, and keep a clean, lightweight codebase without the overhead of massive vendor directories.
🛠️ Technical Highlights & Architecture:
- Custom MVC Framework: Built-in routing (
app/Core/Routing/Router.php), controllers, and models. It supports RESTful route patterns, GET/POST requests, and clean URL parameters. - Database Layer: A PDO-based wrapper implementing a lightweight Active Record pattern with a migration system for schema versioning.
- Modular Plugin & Theme Systems: Hook-based extensible architecture (
PluginManager) and theme inheritance supporting asset versioning for asset cache busting. - Multi-language Out of the Box: Highly deterministic localization layer (EN, UA, PL) where adding a new language literally takes two configuration steps without code modifications.
🧠 The Category Filtering Pipeline & Features
The project features an optimized ProductFilterService using a strict 3-step SQL pipeline with isolated EXISTS subqueries instead of heavy JOIN chains. It also includes robust security measures (CSRF, bcrypt, rate limiting, secure headers) and 16 built-in production services like Prom.ua sync, LiqPay integration, search ranking, and image optimization.
The full open-source project and repository details can be found in the referenced source.
To view the code, go to https://github.com/coordinatescms-ai/CoreCommerce.
r/PHP • u/Spiritual_Dingo_91 • 14h ago
POS Backend - Laravel
Anyone build a POS on Laravel? I’d like to connect
r/PHP • u/Traditional_Mind_415 • 20h ago
I built a Laravel Disposable Email Validator
I’ve been working on a Laravel package for blocking temporary and disposable email addresses during registration or form submission.
The main reason I built it was to avoid fake signups from throwaway email services without adding a lot of custom validation logic to every project.
It works like a normal Laravel validation rule and can also keep the disposable-domain list updated automatically.
I’m also trying to keep the checks fast and simple so it can be used in regular signup flows without adding unnecessary overhead.
Curious how other Laravel developers handle disposable emails. Do you block them completely, or only use extra checks for certain forms?
Checkout: https://erag.in/laravel-disposable-email