Laravel路由可选参数问题
Laravel route optional parameter issue
我的路线是:
Route::get('members/{name?}/{id}', 'Sample1Controller@sampleFn1');
Route::get('members/{id}/edit', 'Sample2Controller@sampleFn2');
当我从 blade 单击 url link 时,
<a href="members/1/edit">Edit</a>
它转到第一条路线并调用Sample1Controller@sampleFn1。为什么??请帮忙..
当我单击 link..我想走第二条路线并调用 Sample2Controller@sampleFn2。有帮助吗?
提前致谢。
它不会根据任何特定约束验证名称或 ID,因此您 /1/edit 符合条件...要使用相同的路由定义,您只需颠倒定义的顺序即可。
尝试以下操作:
Route::get('members/{id}/edit', 'Sample2Controller@sampleFn2');
Route::get('members/{name?}/{id}', 'Sample1Controller@sampleFn1');
您需要在参数中添加路由where条件。我猜第一个是 stings,第二个是整数:
Route::get('members/{name?}/{id}', 'Sample1Controller@sampleFn1')->where([
'name' => '[a-z]+',
'id' => '[0-9]+',
]);
Route::get('members/{id}/edit', 'Sample2Controller@sampleFn2')->where([
'id' => '[0-9]+'
]);
Route::get('/post-delete/{post_id}',[
'uses' => 'PostController@getDeletePost',
'as' => 'post.delete',
'middleware' => 'auth'
]);
我的控制器是这样的
public function getDeletePost($post_id)
{
$post = Post::where('id',$post_id)->first();
if(Auth::user() != $post->user)
{
return redirect()->back();
}
$post->delete();
return redirect()->route('dashboard')->with(['message' => 'Sucessfully Deleted Taunt']);
}
我的blade.php是这样的
<a href="{{ route('post.delete',['post_id' => $post->id]) }}">Delete</a>
以上代码用于删除 u c 尝试编辑
Route::get('members/{name?}/{id}', 'Sample1Controller@sampleFn1');
对于 laravel,URL 看起来都一样,在本例中,$name 被设置为“1”,$id 被设置为 "edit"。
您需要通过将可选参数移动到末尾来避免歧义
状态文本向后一级,在本例中:
Route::get('members/edit/{id}', 'Sample2Controller@sampleFn2');
Route::get('members/{id}/{name?}', 'Sample1Controller@sampleFn1');
我的路线是:
Route::get('members/{name?}/{id}', 'Sample1Controller@sampleFn1');
Route::get('members/{id}/edit', 'Sample2Controller@sampleFn2');
当我从 blade 单击 url link 时,
<a href="members/1/edit">Edit</a>
它转到第一条路线并调用Sample1Controller@sampleFn1。为什么??请帮忙..
当我单击 link..我想走第二条路线并调用 Sample2Controller@sampleFn2。有帮助吗?
提前致谢。
它不会根据任何特定约束验证名称或 ID,因此您 /1/edit 符合条件...要使用相同的路由定义,您只需颠倒定义的顺序即可。
尝试以下操作:
Route::get('members/{id}/edit', 'Sample2Controller@sampleFn2');
Route::get('members/{name?}/{id}', 'Sample1Controller@sampleFn1');
您需要在参数中添加路由where条件。我猜第一个是 stings,第二个是整数:
Route::get('members/{name?}/{id}', 'Sample1Controller@sampleFn1')->where([
'name' => '[a-z]+',
'id' => '[0-9]+',
]);
Route::get('members/{id}/edit', 'Sample2Controller@sampleFn2')->where([
'id' => '[0-9]+'
]);
Route::get('/post-delete/{post_id}',[
'uses' => 'PostController@getDeletePost',
'as' => 'post.delete',
'middleware' => 'auth'
]);
我的控制器是这样的
public function getDeletePost($post_id)
{
$post = Post::where('id',$post_id)->first();
if(Auth::user() != $post->user)
{
return redirect()->back();
}
$post->delete();
return redirect()->route('dashboard')->with(['message' => 'Sucessfully Deleted Taunt']);
}
我的blade.php是这样的
<a href="{{ route('post.delete',['post_id' => $post->id]) }}">Delete</a>
以上代码用于删除 u c 尝试编辑
Route::get('members/{name?}/{id}', 'Sample1Controller@sampleFn1');
对于 laravel,URL 看起来都一样,在本例中,$name 被设置为“1”,$id 被设置为 "edit"。 您需要通过将可选参数移动到末尾来避免歧义 状态文本向后一级,在本例中:
Route::get('members/edit/{id}', 'Sample2Controller@sampleFn2');
Route::get('members/{id}/{name?}', 'Sample1Controller@sampleFn1');