我已经设置了一个Laravel8博客演示应用程序来学习Laravel8的细节。然而,在创建了一个新的控制器(PostsController的副本,称为BatchesController),将资源添加到web.php之后,只有默认的路由工作,比如编辑/创建/销毁。我试着添加一个新的路由,只得到以下错误:"未捕获错误:Ziggy错误:路由'batch.run'不在路由列表中"
batch.run是我的新路由,它几乎是"destroy"的复制。已经为删除帖子设置的路由。我不明白我做错了什么,当我只是简单地使用的东西,是一个接近默认路由的副本。这些路线是在哪里"添加"的?当使用cover-all"资源"时在web.php。它不应该基于路由的名称在控制器中运行任何方法吗?
这是我的web.php:
Auth::routes(['verify' => true]);
Route::get('login', [LoginController::class, 'showLoginForm'])->name('showLoginForm')->middleware('guest');
Route::get('register', [RegisterController::class, 'showRegisterForm'])->name('showRegisterForm')->middleware('guest');
Route::post('login', [LoginController::class, 'authenticate'])->name('login');
Route::post('register', [RegisterController::class, 'register'])->name('register');
Route::post('logout', [LoginController::class, 'logout'])->name('logout');
Route::resource('post', PostsController::class);
Route::resource('batch', BatchesController::class);
Route::resource('token', TokensController::class);
Route::resource('home', HomeController::class);
Route::redirect('/', 'home');
这是我的控制器方法:
public function run(Request $request, $id) {
Batch::find($id)->run();
$request->session()->flash('success', 'Batch run successfully!');
return redirect()->route('batch.running');
}
这是我的。vue文件(减去模板):
<script>
import AppHeader from "../../Partials/AppHeader";
import ErrorsAndMessages from "../../Partials/ErrorsAndMessages";
import {usePage} from "@inertiajs/inertia-vue3";
import {Inertia} from "@inertiajs/inertia";
import {computed, inject} from "vue";
export default {
name: "Batches",
components: {
ErrorsAndMessages,
AppHeader
},
props: {
errors: Object
},
setup() {
const route = inject('$route');
const deleteBatch = (id) => {
if (!confirm('Are you sure want to delete #' + id + '?')) return;
Inertia.delete(route('batch.destroy', {id}));
}
const runBatch = (id) => {
if (!confirm('Are you sure you want to run #' + id + '?')) return;
Inertia.post(route('batch.run', {id}));
}
const batches = computed(() => usePage().props.value.batches);
const numberLinks = batches.value.links.filter((v, i) => i > 0 && i < batches.value.links.length - 1);
return {
batches,
deleteBatch,
runBatch,
numberLinks
}
}
}
</script>
<style scoped>
.action-btn {
margin-left: 12px;
font-size: 13px;
}
.article {
margin-top: 20px;
}
</style>
尝试给路由命名
Route::get('gifts/list','GiftsController@list')->name("gifts.list");
并使用route函数
调用该路由let listUrl = route("gifts.list");
以上解决了我的问题。
此指令:
Route::resource('batch', BatchesController::class);
没有创建名为"batch.run"的路由;自动。只创建这些:[batch。指数,批处理。创建、批处理。商店,批处理。编辑,批处理。
如果你想使用"batch.run"您需要像这样手动创建它:
Route::post('batchRun', [BatchesController::class, 'run'])->name('batch.run');