A
A
Alexander2019-06-17 17:51:21
Laravel
Alexander, 2019-06-17 17:51:21

Is it correct to define routes this way?

Good afternoon.
Please tell me about routing.
If we define a route group

Route::group($groupData, function () {
    //BookCategory
    $methods = ['index', 'edit', 'update', 'create', 'store', 'destroy'];
    Route::resource('categories', 'CategoryController')
        ->only($methods)
        ->names('breh.admin.lib.categories');

    Route::resource('books','BookController')
        ->only($methods)
        ->names('breh.admin.lib.book');
});

That on each delete action it will be necessary to define the form.
Because the route will wait for the DELETE method, not the GET method.
So I created the delete route like this:
Route::get('/admin/categories/{id}/delete', '[email protected]')
    ->middleware('is_admin')
    ->name('breh.admin.lib.categories.delete');

Can you tell me how correct this is?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Aksentiev, 2019-06-17
@Sanasol

Why set it separately if you use Route::resource which supports all this inside of itself?
Implement within the resource.
And where is your delete method here?
The /delete postfix is ​​not needed, because the method will be different, GET /admin/categories/{id} and DELETE /admin/categories/{id} are different routes and without additional divisions.
here is the delete method

J
JhaoDa, 2019-06-17
@JhaoDa

This is so incorrect that it is necessary to chop off hands for this. Maximum POST.
PS The question has been asked and answered a million times on the Internet, I suggest that you first acquire the skills of Google, and only then Laravel.

S
sirenity, 2019-06-18
@sirenity

$methods = ['index', 'edit', 'update', 'create', 'store', 'destroy'];

Since you use all methods except show, you can write except('show'), instead of only($methods)
Does this method render a delete form that is different from an update? And you will delete using the usual method Route::delete('/admin/categories/{id}? then yes, such a record is possible. But then you could leave show and delete there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question