【问题标题】:Symfony\Component\HttpKernel\Exception\NotFoundHttpException in Laravel 5.3Laravel 5.3 中的 Symfony\Component\HttpKernel\Exception\NotFoundHttpException
【发布时间】: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


【解决方案1】:

改变你的正则表达式

where('category', '[w\d\-\_]+');where('slug', '[A-Za-z\d\-\_]+');

以上将解决路由无法正常工作的初始问题。

【讨论】:

    猜你喜欢
    • 2014-06-28
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 2014-08-13
    • 2016-05-21
    • 2021-04-28
    相关资源
    最近更新 更多