'add uri parameter to Route name in laravel

I'm working on a Laravel project, and I made a simple CRUD system, but I have a small problem

to generate the URL system in my project, I made a Route::macro and add it to AppServiceProvider:

Route::macro('crud', function () {
        Route::group([

         ], function () {

            //    Route::resource('', 'CrudController');
            Route::get('{model}', 'CrudController@index');
            Route::get('{model}/create', 'CrudController@create');

            Route::post('{model}', 'CrudController@store');             /** << post **/

            Route::get('{model}/{id}', 'CrudController@show');
            Route::get('{model}/{id}/edit', 'CrudController@edit');

            Route::match(['PUT', 'PATCH'],'{model}/{id}', 'CrudController@update'); /** << post **/
            Route::delete('{model}/{id}', 'CrudController@destroy'); /** << post **/
        });
    });

this works great so far, but the issue is I need to use ->name() with it, and adding the $model parameter to it!

example of what I'm trying to do:

Route::get('{model}', 'CrudController@index')->name('{model}.index');

is it possible?, Thanks in advance



Solution 1:[1]

You can get all models names and loop through them and add the model name to the route name prefix at runtime

loop (model in models) 
   Route::get("{model}","Atcion")->name("{model}.index")
endloop

I hope my answare helps you in your project

Solution 2:[2]

Here in this example you can loop over of some numbers and dynamically create some routes:

 for ($i = 0; $i < 5; $i++) {
     Route::get('test/' . $i, 'Controller@test_' . $i)->name('test.' . $i);
 }

You can check that all added routes with "php artisan route:list". I don't recommend you to do that, but for your case you can somewhere in routes.php define array like this, and loop over on that:

 $models = ['user', 'owner', 'admin'];
 foreach ($models as $model) {
     Route::get($model, 'CrudController@index')->name($model . '.index');
 }

Or you can define that array in configs (for example in "config/app.php") like:

 'models' => ['user', 'owner', 'admin'];

And in routes.php you can just retrieve that with this (don't forget to run "php artisan config:cache" after changing app.php):

 $models = config('app.models');
 // foreach loop

Solution 3:[3]

You can prefix the name to a group of routes for the model
From the docs

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Route::macro('crud', function () {
            Route::name('{model}.')->group(function () {
                Route::get('{model}', 'CrudController@index')->name('index');
                Route::get('{model}/create', 'CrudController@create')->name('create');
                Route::post('{model}', 'CrudController@store')->name('store');
                Route::get('{model}/{id}', 'CrudController@show')->name('show');
                Route::get('{model}/{id}/edit', 'CrudController@edit')->name('edit');
                Route::match(['PUT', 'PATCH'], '{model}/{id}', 'CrudController@update')->name('update');
                Route::delete('{model}/{id}', 'CrudController@destroy')->name('destroy');
            });
        });
    }
}

Result

+-----------+----------------------------+-----------------+------------------------------------------------------------+
| Method    | URI                        | Name            | Action                                                     |
+-----------+----------------------------+-----------------+------------------------------------------------------------+
| GET|HEAD  | {model}                    | {model}.index   | App\Http\Controllers\CrudController@index                  |
| POST      | {model}                    | {model}.store   | App\Http\Controllers\CrudController@store                  |
| GET|HEAD  | {model}/create             | {model}.create  | App\Http\Controllers\CrudController@create                 |
| GET|HEAD  | {model}/{id}               | {model}.show    | App\Http\Controllers\CrudController@show                   |
| PUT|PATCH | {model}/{id}               | {model}.update  | App\Http\Controllers\CrudController@update                 |
| DELETE    | {model}/{id}               | {model}.destroy | App\Http\Controllers\CrudController@destroy                |
| GET|HEAD  | {model}/{id}/edit          | {model}.edit    | App\Http\Controllers\CrudController@edit                   |
+-----------+----------------------------+-----------------+------------------------------------------------------------+

Now you can access example.com/user/create and example.com/product/create and they both take to the same controller method

I hope this helps

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Youssef mahmoed
Solution 2
Solution 3