如何在 Route::group 中指定带有参数的 Route::get?
How can I specify a Route::get with a parameter inside a Route::group?
出于某种原因,我无法让它工作,我已经绞尽脑汁两天了,试图弄清楚它只是没有发生。
这是我设置的路由组...
// Admin Panel Routes Group
Route::group(['prefix' => 'admin', 'middleware' => ['auth']], function() {
Route::get('/', ['uses' => 'Admin\AdminController@index'])->name('admin');
Route::get('posts', ['uses' => 'PostController@index'])->name('posts-list');
Route::delete('post/delete/{id}', ['uses' => 'PostController@delete'])->name('post-delete');
Route::get('posts/new', ['uses' => 'PostController@newPost'])->name('post-new');
Route::post('posts/create', ['uses' => 'PostController@createPost'])->name('post-create');
Route::post('posts/upload', ['uses' => 'PostController@fileUpload'])->name('post-upload');
Route::get('post/{slug}/edit', ['uses' => 'PostController@editPost'])->name('post-edit');
});
如您所见,我成功地在 post 的 DELETE 请求中使用了一个参数,效果非常好。 PostController 中的 DELETE 请求函数按预期工作并从 URL 中读取参数并从数据库中删除记录。
问题可能出在 PostController 'editPost' 函数中,这是我打算用来通过 slug 从数据库中获取 post 的函数,使用它来确定哪个 post 是,然后用数据加载表单。
public function editPost($slug) {
$post = Post::whereSlug($slug)->firstOrFail();
return view('posts.edit')->withPost($post);
}
我正在考虑改用 ID,但我认为这不是我的问题所在。删除函数和我拥有的这个新编辑函数之间的唯一区别是它使用字符串来获取 post 而不是 ID.
我遇到的问题是在尝试访问特定 post 的编辑表单时...
(1/2) UrlGenerationException
Missing required parameters for [Route: post-edit] [URI: admin/post/{slug}/edit].
欢迎提出改进代码的建议,我最近才开始使用 Laravel,这是我的第一个项目。
为了以防万一有人要求,这是 'edit.blade.php' 我正在将 $post 变量传递给...
@extends('layouts.panel')
@section('content')
<form class="new-post" method="POST" action="">
{{ csrf_field() }}
<div class="new-post__seperator">
<input type="text" class="new-post__input" id="title" name="title" value="{{ $post->title }}" placeholder="Post Title..." autofocus>
</div>
<div class="new-post__upload"></div>
<div class="new-post__editor">
<textarea id="wysiwyg" value="{{ $post->content }}"></textarea>
</div>
<div class="new-post__checkboxes">
<input type="checkbox" id="publish-checkbox" name="publish-checkbox" class="publish-checkbox" checked>
<label for="publish-checkbox">By checking this box, you will be publishing the post, making it viewable via the home page.</label>
</div>
</form>
@endsection
尝试输出 slug 变量,看看它是什么 returns
问题应该是您在代码或视图中的某处使用 route('post-edit')
而不是 route('post-edit', $slug)
。你能检查一下吗?
我已经设法找到了答案,在我的 'layouts/panel.blade.php' 中,我有一个 link 到我的 'posts-edit' 路由,它根本没有向它传递任何变量,所以正在点击错误。
感谢@aynber 的评论解决了我的问题..
It should be fine accessing the link, yes. However, from what I've seen, that error comes when generating a link on the page using route(). Link the first, Link the second, Link the third
出于某种原因,我无法让它工作,我已经绞尽脑汁两天了,试图弄清楚它只是没有发生。
这是我设置的路由组...
// Admin Panel Routes Group
Route::group(['prefix' => 'admin', 'middleware' => ['auth']], function() {
Route::get('/', ['uses' => 'Admin\AdminController@index'])->name('admin');
Route::get('posts', ['uses' => 'PostController@index'])->name('posts-list');
Route::delete('post/delete/{id}', ['uses' => 'PostController@delete'])->name('post-delete');
Route::get('posts/new', ['uses' => 'PostController@newPost'])->name('post-new');
Route::post('posts/create', ['uses' => 'PostController@createPost'])->name('post-create');
Route::post('posts/upload', ['uses' => 'PostController@fileUpload'])->name('post-upload');
Route::get('post/{slug}/edit', ['uses' => 'PostController@editPost'])->name('post-edit');
});
如您所见,我成功地在 post 的 DELETE 请求中使用了一个参数,效果非常好。 PostController 中的 DELETE 请求函数按预期工作并从 URL 中读取参数并从数据库中删除记录。
问题可能出在 PostController 'editPost' 函数中,这是我打算用来通过 slug 从数据库中获取 post 的函数,使用它来确定哪个 post 是,然后用数据加载表单。
public function editPost($slug) {
$post = Post::whereSlug($slug)->firstOrFail();
return view('posts.edit')->withPost($post);
}
我正在考虑改用 ID,但我认为这不是我的问题所在。删除函数和我拥有的这个新编辑函数之间的唯一区别是它使用字符串来获取 post 而不是 ID.
我遇到的问题是在尝试访问特定 post 的编辑表单时...
(1/2) UrlGenerationException Missing required parameters for [Route: post-edit] [URI: admin/post/{slug}/edit].
欢迎提出改进代码的建议,我最近才开始使用 Laravel,这是我的第一个项目。
为了以防万一有人要求,这是 'edit.blade.php' 我正在将 $post 变量传递给...
@extends('layouts.panel')
@section('content')
<form class="new-post" method="POST" action="">
{{ csrf_field() }}
<div class="new-post__seperator">
<input type="text" class="new-post__input" id="title" name="title" value="{{ $post->title }}" placeholder="Post Title..." autofocus>
</div>
<div class="new-post__upload"></div>
<div class="new-post__editor">
<textarea id="wysiwyg" value="{{ $post->content }}"></textarea>
</div>
<div class="new-post__checkboxes">
<input type="checkbox" id="publish-checkbox" name="publish-checkbox" class="publish-checkbox" checked>
<label for="publish-checkbox">By checking this box, you will be publishing the post, making it viewable via the home page.</label>
</div>
</form>
@endsection
尝试输出 slug 变量,看看它是什么 returns
问题应该是您在代码或视图中的某处使用 route('post-edit')
而不是 route('post-edit', $slug)
。你能检查一下吗?
我已经设法找到了答案,在我的 'layouts/panel.blade.php' 中,我有一个 link 到我的 'posts-edit' 路由,它根本没有向它传递任何变量,所以正在点击错误。
感谢@aynber 的评论解决了我的问题..
It should be fine accessing the link, yes. However, from what I've seen, that error comes when generating a link on the page using route(). Link the first, Link the second, Link the third