PHP code example of mmghv / lumen-route-binding

1. Go to this page and download the library: Download mmghv/lumen-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/ */

    

mmghv / lumen-route-binding example snippets


// app/Providers/RouteBindingServiceProvider.php

namespace App\Providers;

use mmghv\LumenRouteBinding\RouteBindingServiceProvider as BaseServiceProvider;

class RouteBindingServiceProvider extends BaseServiceProvider
{
    /**
     * Boot the service provider
     */
    public function boot()
    {
        // The binder instance
        $binder = $this->binder;

        // Here we define our bindings
    }
}

$app->register(App\Providers\RouteBindingServiceProvider::class);

$binder->bind('user', 'App\User');

$router->get('profile/{user}', function(App\User $user) {
    //
});

$instance = new App\User;
return $instance->where($instance->getRouteKeyName(), $value)->firstOrFail();

/**
 * Get the route key for the model.
 *
 * @return string
 */
public function getRouteKeyName()
{
    return 'slug';
}

// Using a 'Class@method' callable style
$binder->bind('article', 'App\Article@findForRoute');

// Using a closure
$binder->bind('article', function($value) {
    return \App\Article::where('slug', $value)->firstOrFail();
});

$binder->bind('article', 'App\Article', function($e) {
    // We can return a default value if the model not found :
    return new \App\Article();

    // Or we can throw another exception for example :
    throw new \NotFoundHttpException;
});

$binder->implicitBind('App');

$router->get('articles/{article}', function($myArticle) {
    //
});

/**
 * Get the route key for the model.
 *
 * @return string
 */
public function getRouteKeyName()
{
    return 'slug';
}

$binder->implicitBind('App\Repositories', 'Eloquent', 'Repository');

$router->get('articles/{article}', function($myArticle) {
    //
});

$binder->implicitBind('App\Repositories', 'Eloquent', 'Repository', 'findForRoute');

$binder->implicitBind('App\Repositories', '', 'Repository', 'findForRoute');

$router->get('articles/{article}', function(App\Article $article) {
    return view('articles.view', compact('article'));
});

/**
 * Find the Article for route-model-binding
 *
 * @param  string $val  wildcard value
 *
 * @return \App\Article
 */
public function findForRoute($val)
{
    return $this->model->where('slug', $val)->firstOrFail();
}

$router->get('posts/{post}/comments/{comment}', function(App\Post $post, App\Comment $comment) {
    //
});

// Using a 'Class@method' callable style
$binder->compositeBind(['post', 'comment'], 'App\Repositories\PostRepository@findPostCommentForRoute');

// Using a closure
$binder->compositeBind(['post', 'comment'], function($postKey, $commentKey) {
    $post = \App\Post::findOrFail($postKey);
    $comment = $post->comments()->findOrFail($commentKey);

    return [$post, $comment];
});

$app->register(Dingo\Api\Provider\LumenServiceProvider::class);

$app->register(mmghv\LumenRouteBinding\DingoServiceProvider::class);

> php >= 7.1
> Lumen 5 - 10
>