【发布时间】:2016-12-07 07:29:15
【问题描述】:
以前我遇到过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>
【问题讨论】:
-
你能给我们一个每条路线的示例网址吗?就像
example.com/england/outdoor? -
@scottevans93 第一路线的网址:
http://localhost:8000/Croatia/Cycling第二路线的网址:http://localhost:8000/Cycling/beach-side-cycling -
这两个 url 产生相同的 HTTP 错误?
-
只有第二条路线给出了错误。第一条路线还可以
-
应该
where('category', '[w\d\-\_]+');不是where('slug', '[w\d\-\_]+');?
标签: php laravel blade laravel-5.3 laravel-blade