You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Laravel中使用资源控制器自定义动作与路由名称(替换index为browse、destroy为delete)?

How to Customize Laravel Resource Controller Actions and Route Names

If you want to tweak Laravel’s default resource controller setup—renaming the index action to browse and destroy to delete—while keeping the clean Route::resource() syntax (instead of writing individual routes), here’s exactly how to do it:

1. Update Your Controller Methods

First, rename the corresponding methods in your ResourceController to match your desired action names:

// app/Http/Controllers/ResourceController.php
public function browse()
{
    // Drop your original index() logic here
}

// Keep all other default methods (create, store, show, edit, update) as-is

public function delete($id)
{
    // Drop your original destroy() logic here
}

2. Customize the Resource Route Mapping

In your routes/web.php (or routes/api.php for API routes), define the resource route with custom route names and controller method mappings using the names and actions options:

// routes/web.php
Route::resource('resource', 'ResourceController', [
    // Map default route names to your custom identifiers
    'names' => [
        'index' => 'resource.browse',
        'destroy' => 'resource.delete'
    ],
    // Link default resource actions to your renamed controller methods
    'actions' => [
        'index' => 'ResourceController@browse',
        'destroy' => 'ResourceController@delete'
    ]
]);

3. Verify the Changes

Run this Artisan command to confirm your routes are updated correctly:

php artisan route:list

You’ll see the final mapping looks like this (simplified):

HTTP MethodURIController ActionRoute Name
GET/resourcebrowseresource.browse
GET/resource/createcreateresource.create
POST/resourcestoreresource.store
GET/resource/{resource}showresource.show
GET/resource/{resource}/editeditresource.edit
PUT/PATCH/resource/{resource}updateresource.update
DELETE/resource/{resource}deleteresource.delete

This approach lets you keep the concise resource route syntax while fully customizing action names and route identifiers to fit your project’s needs.

内容的提问来源于stack exchange,提问作者Vadim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 04:08:10