PHP code example of illuminatech / model-route

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

    

illuminatech / model-route example snippets




namespace App\Http\Controllers;

use Illuminate\Support\Facades\Route;

Route::get('{user}', UserController::class.'@show')->name('users.show');
Route::get('{organization}', OrganizationController::class.'@show')->name('organizations.show');



namespace App\Http\Controllers;

use App\Models\User;

class UserController extends Controller
{
    public function show(User $user)
    {
        // ...
    }
}

use App\Models\Organization;

class OrganizationController extends Controller
{
    public function show(Organization $organization)
    {
        // ...
    }
}



namespace App\Providers;

use Illuminatech\ModelRoute\ModelRouteValidator;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    public function boot()
    {
        (new ModelRouteValidator)
            ->setBinders([
                'user' => \App\Models\User::class.'@username',
                'organization' => \App\Models\Organization::class.'@name',
            ])
            ->register();

        parent::boot();
    }

    // ...
}



use Illuminatech\ModelRoute\ModelRouteValidator;

(new ModelRouteValidator)
    ->setBinders([
        'blog' => \App\Models\BlogPost::class, // search using `\App\Models\BlogPost::getRouteKeyName()`
        'item' => \App\Models\Item::class.'@slug', // search using `\App\Models\Item::$slug`
        'project' => function ($value) {
            return \App\Models\Project::query()->where('name', $value)->first(); // if not found - `null` will be returned
        },
    ])
    ->register();



namespace App\Http\Controllers;

use Illuminate\Support\Facades\Route;

// predefined site sections should be described beforehand:
Route::view('about', 'pages/about')->name('about');
Route::view('privacy-policy', 'pages/privacy-policy')->name('privacy-policy');

Route::get('blog', BlogController::class.'@index')->name('blog.index');
Route::get('blog/{blogArticle}', BlogController::class.'@show')->name('blog.show');

// only once all other routes are defined, we can use dynamic binding:
Route::get('{user}', UserController::class.'@show')->name('users.show'); // matching check will cause a DB query against model `App\Models\User`
Route::get('{organization}', OrganizationController::class.'@show')->name('organizations.show'); // matching check will cause a DB query against model `App\Models\Organization`



use Illuminatech\ModelRoute\ModelRouteValidator;

(new ModelRouteValidator)
    ->setBinders([
        'user' => \App\Models\User::class.'@username',
        'organization' => \App\Models\Organization::class.'@name',
    ])
    ->setIgnoredUrlPaths([
        config('telescope.path'), // exclude Telescope URLs
        config('horizon.path'), // exclude Horizon URLs
        config('nova.path'), // exclude Nova URLs
        'nova-api', // exclude Nova API URLs
    ])
    ->register();