在 Laravel 5.5 中通过名称获取路由 URL 时,有没有办法指定前缀?例如,
在 routes/web.php
Route::resource('users', 'UserController', ['only' => 'index', 'create', 'edit']);
在
routes/api.php
Routes::resource('users', 'UserController', ['only' => 'index', 'store', 'update', 'destroy']);
索引方法的两个路由都具有名称
users.index
,在调用 php artisan route:list
时确认.但是,Web 路由的 URL 是 /users
api 路由的 URL 是 /api/users
.要获取网络路由 URL,我可以执行
route('users.index')
.有没有办法使用路由名称获取 api 路由的 URL?
最佳答案
您可以通过 as
为资源 Controller 的路由提供前缀。选项:
Route::resource('users', 'UserController', [
'as' => 'your.prefix',
'only' => ['index', 'store', 'update', 'destroy'],
]);
关于php - 在 Laravel 5.5 中通过路由名称获取 API URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47331778/