laravel-code-generator icon indicating copy to clipboard operation
laravel-code-generator copied to clipboard

Change routes name

Open thewebartisan7 opened this issue 5 years ago • 4 comments

Is your feature request related to a problem? Please describe.

When I create a scaffold for a model name "Dummy", the route name are like:

'dummies.dummy.index' 'dummies.dummy.create' etc...

Describe the solution you'd like

I think should be like:

'dummies.index' 'dummies.create' etc...

Or am I missing something?

I run the following commands, without any customization of config or anything else:

php artisan resource-file:create Dummy --fields=id,name,description,folder,filename,is_active
php artisan create:scaffold Dummy --with-migration

Also since there is usage of group(), name base could be added like so:



Route::group([
    'prefix' => 'dummies',
    'name' => 'dummies.',
], function () {
    Route::get('/', 'DummiesController@index')
         ->name('index');
    Route::get('/', 'DummiesController@index')
         ->name('create');
//...etc...
});


thewebartisan7 avatar Jul 08 '20 04:07 thewebartisan7

Another issue related to generated routes. The regular expression constraints doesn't work since doesn't match the name of resource.

This:

    Route::get('/show/{dummies}','DummiesController@show')
         ->name('dummies.dummy.show')->where('id', '[0-9]+');

Should be:

    Route::get('/show/{dummies}','DummiesController@show')
         ->name('dummies.dummy.show')->where('dummies', '[0-9]+');

Or:

    Route::get('/show/{id}','DummiesController@show')
         ->name('dummies.dummy.show')->where('id', '[0-9]+');

Or just remove it and can be added globally in RouteServiceProvider:

    public function boot()
    {
        Route::pattern('id','[0-9]+');
    
        parent::boot();
    }

thewebartisan7 avatar Aug 06 '20 04:08 thewebartisan7

I forgot one thing.

A simple solution could be also just single line for each resource like:

        Route::resource('dummies', 'DummiesController');

Also update, show and delete routes better to keep like standard param just after resource name. Actually only "edit" follow this convention. And "store" missing "store" at the end of url. And destroy rename to "delete" So it's like generated routes of Route::resource()

Example:


    Route::get('/dummies/{dummy}/show/,'DummiesController@show')
         ->name('dummies.dummy.show');

    Route::get('/dummies/{dummy}/edit','DummiesController@edit')
         ->name('dummies.dummy.edit')->where('id', '[0-9]+');

    Route::post('/dummies/store', 'DummiesController@store')
         ->name('dummies.dummy.store');

    Route::put('dummy/{dummy}/update', 'DummiesController@update')
         ->name('dummies.dummy.update');

    Route::delete('/dummy/{dummy}/delete','DummiesController@delete')
         ->name('dummies.dummy.delete');

thewebartisan7 avatar Aug 06 '20 04:08 thewebartisan7

I could not find where to ask a question. It seems my implementation will not do deletes. Could you point me in the right direction? crestapps/laravel-code-generator v2.4.7 v8.80.0 The Laravel Framework.

WEB.PHP Route::delete('/country/{country}',[CountriesController::class, 'index']) ->name('countries.country.destroy')->where('id', '[0-9]+');

VIEW

{{ csrf_field() }}

CONTROLLER public function update($id, Request $request) { try {

        $data = $this->getData($request);
        
        $country = Country::findOrFail($id);
        $country->update($data);

        return redirect()->route('countries.country.index')
            ->with('success_message', 'Country was successfully updated.');
    } catch (Exception $exception) {

        return back()->withInput()
            ->withErrors(['unexpected_error' => 'Unexpected error occurred while trying to process your request.']);
    }        
}

Thanks...

levyra avatar Feb 02 '22 02:02 levyra

Is there a benefit of changing this?

MikeAlhayek avatar Jan 27 '24 23:01 MikeAlhayek