routes/web 将我带到另一条路线
routes/web route me to another route
我的网站似乎没有指向正确的页面。
这是我的 blade
<div class="container">
<div class="col-lg-12 d-flex justify-content-between align-items-center">
<h2>Informations</h2>
<a href="{{ route('add-new-information') }}" class="btn text-success">
<i class="fas fa-plus-circle fa-2x"></i>
</a>
</div>
<hr>
<div class="row d-flex justify-content-center align-items-center mt-5" style="max-height: 500px !important; overflow-y: scroll">
@foreach ($informations as $info)
<div class="card col-sm-11 p-0 mb-4 clickable-item" onclick='window.location = "{{ route('admin-informations', ['id' => $info->id]) }}"'>
...
</div>
@endforeach
</div>
</div>
这是我的 routes/web
Auth::routes();
Route::group(['middleware' => ['auth'], 'prefix' => '/',], function () {
Route::get('/', function () {
return redirect('/home');
});
Route::group(['prefix' => 'admin'], function() {
Route::get('/informations', [App\Http\Controllers\InformationController::class, 'index'])->name('informations');
Route::get('/informations/{id}', [App\Http\Controllers\InformationController::class, 'indexAdminInfo'])->name('admin-informations');
Route::get('/informations/add-new-information', [App\Http\Controllers\InformationController::class, 'add'])->name('add-new-information');
});
});
这是我的控制器
public function indexAdminInfo($id){
$information = Information::find($id);
// $comments = Comment::where('information_id', $id)->orderByDesc('created_at')->get();
$ack_count = Acknowledge::where('information_id', $id)->count();
$user_ack = Acknowledge::where([
['information_id', '=', $id],
['user_id', '=', Auth::user()->id],
])->first();
$ack = 'FALSE';
if($user_ack != null){
$ack = 'TRUE';
}
return view('adminviews/infoadmin', compact('information', 'ack_count', 'ack', 'user_ack'));
}
public function add(){
return view('adminviews/addinfo');
}
出于某种原因,当我点击带 href {{ route('add-new-information') }}
的标签进入添加页面时 'adminviews/addinfo'
,
相反,页面将转至 'adminviews/infoadmin' page
,这将导致错误,因为未发送任何参数。
我尝试检查代码,但我认为它是正确的。有人能找出其中的错误吗?
问题出在你的路线上:
这两条路线不明确:
Route::get('/informations/{id}');
Route::get('/informations/add-new-information');
想想下面的场景:
路由器要路由,这个url : /information/add-new-information
路由器将命中第一个定义的路由,因为它与定义兼容('/informations/{id}'
)
Note :{id}
is a variable and can be any string
所以它会随之而来。
解决方案
先写比较受限制的路线,
以及稍后更通用的路线:
Route::get('/informations/add-new-information');
Route::get('/informations/{id}');
我的网站似乎没有指向正确的页面。
这是我的 blade
<div class="container">
<div class="col-lg-12 d-flex justify-content-between align-items-center">
<h2>Informations</h2>
<a href="{{ route('add-new-information') }}" class="btn text-success">
<i class="fas fa-plus-circle fa-2x"></i>
</a>
</div>
<hr>
<div class="row d-flex justify-content-center align-items-center mt-5" style="max-height: 500px !important; overflow-y: scroll">
@foreach ($informations as $info)
<div class="card col-sm-11 p-0 mb-4 clickable-item" onclick='window.location = "{{ route('admin-informations', ['id' => $info->id]) }}"'>
...
</div>
@endforeach
</div>
</div>
这是我的 routes/web
Auth::routes();
Route::group(['middleware' => ['auth'], 'prefix' => '/',], function () {
Route::get('/', function () {
return redirect('/home');
});
Route::group(['prefix' => 'admin'], function() {
Route::get('/informations', [App\Http\Controllers\InformationController::class, 'index'])->name('informations');
Route::get('/informations/{id}', [App\Http\Controllers\InformationController::class, 'indexAdminInfo'])->name('admin-informations');
Route::get('/informations/add-new-information', [App\Http\Controllers\InformationController::class, 'add'])->name('add-new-information');
});
});
这是我的控制器
public function indexAdminInfo($id){
$information = Information::find($id);
// $comments = Comment::where('information_id', $id)->orderByDesc('created_at')->get();
$ack_count = Acknowledge::where('information_id', $id)->count();
$user_ack = Acknowledge::where([
['information_id', '=', $id],
['user_id', '=', Auth::user()->id],
])->first();
$ack = 'FALSE';
if($user_ack != null){
$ack = 'TRUE';
}
return view('adminviews/infoadmin', compact('information', 'ack_count', 'ack', 'user_ack'));
}
public function add(){
return view('adminviews/addinfo');
}
出于某种原因,当我点击带 href {{ route('add-new-information') }}
的标签进入添加页面时 'adminviews/addinfo'
,
相反,页面将转至 'adminviews/infoadmin' page
,这将导致错误,因为未发送任何参数。
我尝试检查代码,但我认为它是正确的。有人能找出其中的错误吗?
问题出在你的路线上: 这两条路线不明确:
Route::get('/informations/{id}');
Route::get('/informations/add-new-information');
想想下面的场景:
路由器要路由,这个url : /information/add-new-information
路由器将命中第一个定义的路由,因为它与定义兼容('/informations/{id}'
)
Note :
{id}
is a variable and can be any string
所以它会随之而来。
解决方案
先写比较受限制的路线,
以及稍后更通用的路线:
Route::get('/informations/add-new-information');
Route::get('/informations/{id}');