以前我遇到route precedence的问题 有帮助和建议我通过在我的路线中添加正则表达式来克服它。现在我的路线是:
Route::get('/{country}/{category}', ['as' => 'tour.list', 'uses' => 'LinkController@tourlist'])
->where('country', '[A-Za-z]+')->where('category', '[A-Za-z]+');
Route::get('/{category}/{slug}',['as' => 'single.tour', 'uses' => 'LinkController@singleTour'])
->where('category', '[A-Za-z]+')->where('category', '[w\d\-\_]+');
使用此路由我收到错误:
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
当我从第一条路线移除正则表达式时,我遇到了与之前相同的问题,当我从第二条路线移除正则表达式时,我收到错误:
Trying to get property of non-object
(View: F:\project\resources\views\public\tours\show.blade.php)
我在LinkController中的方法是:
public function tourlist($country, $category){
$tour = Tour::whereHas('category', function($q) use($category) {
$q->where('name','=', $category);
})
->whereHas('country', function($r) use($country) {
$r->where('name','=', $country);
})
->get();
return view('public.tours.list')->withTours($tour);
}
public function singleTour($slug,$category)
{
$tour = Tour::where('slug','=', $slug)
->whereHas('category', function($r) use($category) {
$r->where('name','=', $category);
})
->first();
return view('public.tours.show')->withTour($tour);
}
我的代码是:
<a href="{{ route('single.tour',['category' => $tour->category->name, 'slug' => $tour->slug]) }}">{{$tour->title}}</a>
答案 0 :(得分:0)
更改正则表达式
where('category', '[w\d\-\_]+');
至where('slug', '[A-Za-z\d\-\_]+');
以上将解决路线无法正常工作的初始问题。