PHP code example of umutcangungormus / laravel-named-route-binding

1. Go to this page and download the library: Download umutcangungormus/laravel-named-route-binding library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

umutcangungormus / laravel-named-route-binding example snippets


// routes/web.php
Route::get('/users/{user}/posts/{post}', [PostController::class, 'show']);

// PostController.php - Parametreler SIRAYLA gelmeli!
public function show($user, $post)
{
    // $user = {user} route parametresi
    // $post = {post} route parametresi
}

// ❌ Bu ÇALIŞMAZ! Sıra yanlış olduğu için $post'a user değeri gelir
public function show($post, $user)
{
    // $post = {user} route parametresi (YANLIŞ!)
    // $user = {post} route parametresi (YANLIŞ!)
}

// routes/web.php
Route::get('/users/{user}/posts/{post}', [PostController::class, 'show']);

// PostController.php - Artık sıra önemli DEĞİL!
public function show($post, $user)
{
    // ✅ $post = {post} route parametresi (İSİMLE EŞLEŞTİ!)
    // ✅ $user = {user} route parametresi (İSİMLE EŞLEŞTİ!)
}

// İstediğiniz sırada yazabilirsiniz
public function show($user, $post) // ✅ Çalışır
public function show($post, $user) // ✅ Çalışır

// config/app.php
'providers' => [
    // ...
    UmutcanGungormus\NamedRouteBinding\NamedRouteBindingServiceProvider::class,
],

return [
    // Özelliği etkinleştir/devre dışı bırak
    'enabled' => env('NAMED_ROUTE_BINDING_ENABLED', true),
];

Route::get('/categories/{category}/products/{product}', [ProductController::class, 'show']);

// Her iki yazım da çalışır
public function show($category, $product) { }
public function show($product, $category) { }

Route::get('/users/{user_id}', [UserController::class, 'show']);

// Her ikisi de çalışır
public function show($user_id) { }
public function show($userId) { }

Route::get('/users/{user}', [UserController::class, 'show']);

public function show(Request $request, $user)
{
    // $request otomatik enjekte edilir
    // $user route parametresinden gelir
}

// Sıra değiştirilebilir
public function show($user, Request $request) { }

Route::get('/posts/{post}', [PostController::class, 'show']);

public function show($post, $format = 'json')
{
    // $format varsayılan değeri kullanır
}

public function show($post, ?string $optional)
{
    // $optional null olur eğer route'da yoksa
}

Route::get('/users/{user}/posts/{post}', [PostController::class, 'show']);

// Type-hinted modeller otomatik çözümlenir
public function show(Post $post, User $user)
{
    // $post ve $user model instance'ları olarak gelir
}

// routes/web.php
Route::get('/shops/{shop}/categories/{category}/products/{product}', [ProductController::class, 'show']);

// ProductController.php - İstediğiniz sırada
public function show(
    Request $request,
    Product $product,    // 3. route parametresi
    Category $category,  // 2. route parametresi  
    Shop $shop          // 1. route parametresi
) {
    // Hepsi doğru şekilde eşleşir!
}

// routes/api.php
Route::get('/teams/{team}/members/{member}/tasks/{task}', [TaskController::class, 'show']);

// TaskController.php
public function show(Task $task, Member $member, Team $team)
{
    $this->authorize('view', [$task, $team]);
    
    return new TaskResource($task);
}

// routes/web.php
Route::get('/users/{user}/posts/{post}', [PostController::class, 'show']);

// PostController.php - Parameters must be IN ORDER!
public function show($user, $post)
{
    // $user = {user} route parameter
    // $post = {post} route parameter
}

// ❌ This DOESN'T WORK! Wrong order means $post gets user value
public function show($post, $user)
{
    // $post = {user} route parameter (WRONG!)
    // $user = {post} route parameter (WRONG!)
}

// routes/web.php
Route::get('/users/{user}/posts/{post}', [PostController::class, 'show']);

// PostController.php - Order NO LONGER matters!
public function show($post, $user)
{
    // ✅ $post = {post} route parameter (MATCHED BY NAME!)
    // ✅ $user = {user} route parameter (MATCHED BY NAME!)
}

// Write them in any order you want
public function show($user, $post) // ✅ Works
public function show($post, $user) // ✅ Works

// config/app.php
'providers' => [
    // ...
    UmutcanGungormus\NamedRouteBinding\NamedRouteBindingServiceProvider::class,
],

return [
    // Enable/disable the feature
    'enabled' => env('NAMED_ROUTE_BINDING_ENABLED', true),
];

Route::get('/categories/{category}/products/{product}', [ProductController::class, 'show']);

// Both work
public function show($category, $product) { }
public function show($product, $category) { }

Route::get('/users/{user_id}', [UserController::class, 'show']);

// Both work
public function show($user_id) { }
public function show($userId) { }

Route::get('/users/{user}', [UserController::class, 'show']);

public function show(Request $request, $user)
{
    // $request is auto-injected
    // $user comes from route parameter
}

// Order can be changed
public function show($user, Request $request) { }

Route::get('/posts/{post}', [PostController::class, 'show']);

public function show($post, $format = 'json')
{
    // $format uses default value
}

public function show($post, ?string $optional)
{
    // $optional is null if not in route
}

Route::get('/users/{user}/posts/{post}', [PostController::class, 'show']);

// Type-hinted models are automatically resolved
public function show(Post $post, User $user)
{
    // $post and $user come as model instances
}

// routes/web.php
Route::get('/shops/{shop}/categories/{category}/products/{product}', [ProductController::class, 'show']);

// ProductController.php - Any order you want
public function show(
    Request $request,
    Product $product,    // 3rd route parameter
    Category $category,  // 2nd route parameter  
    Shop $shop          // 1st route parameter
) {
    // All matched correctly!
}

// routes/api.php
Route::get('/teams/{team}/members/{member}/tasks/{task}', [TaskController::class, 'show']);

// TaskController.php
public function show(Task $task, Member $member, Team $team)
{
    $this->authorize('view', [$task, $team]);
    
    return new TaskResource($task);
}
bash
php artisan vendor:publish --provider="UmutcanGungormus\NamedRouteBinding\NamedRouteBindingServiceProvider"
bash
php artisan vendor:publish --provider="UmutcanGungormus\NamedRouteBinding\NamedRouteBindingServiceProvider"