重定向到嵌套控制器操作

Redirect to nested controller action

我最近将我的控制器移到了 Admin 目录。

在我的控制器中,我重定向到控制器的 index() 操作。

return redirect()->action('ServiceController@index');

现在我得到以下错误:

InvalidArgumentException Action App\Http\Controllers\ServiceController@index not defined.

我不知道如何在文档中声明新的操作重定向,所以我在这里发布我的问题。

路线

Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function () {
    Route::resource('projects', 'ProjectController');
    Route::resource('services', 'ServiceController');
    Route::resource('projectFiles', 'ProjectFileController');
    Route::get('seed', 'SeedController@seedDatabase')->name('seed');
});

这就是我所说的控制器部分:

class ServiceController extends Controller
{
    public function index()
    {
        return view('admin.services.index', [
            'services' => Service::all()
        ]);
    }

    public function create()
    {
        return view('admin.services.create');
    }

    public function store(Request $request)
    {
        try {
            Service::create([
                'name' => $request->name,
                'machine_name' => snake_case($request->name),
                'description' => $request->description
            ]);

            return redirect()->action('\App\Htpp\Controllers\Admin\ServiceController@index');
        } catch (\Throwable $th) {
            throw $th;
        }
    }
}

我想我找到了答案,但如果我错了,任何人都可以纠正我。

RouteServiceProvider 中,命名空间设置为 App\Http\Controllers:

protected $namespace = 'App\Http\Controllers';

所以我决定在控制器名称前添加 Admin\,现在重定向起作用了:

return redirect()->action('Admin\ServiceController@index');