php - 当 slug 不在数据库中时,Laravel route /{slug} 错误
问题描述
我在 web.php 中有一条路线
Route::get('/{slug_category}', 'WelcomeController@cdetail')->name('cdetail');
Route::get('/{slug_category}/{slug}', 'WelcomeController@detail')->name('detail');
我在数据库中的类别 slug 是action, movie
当我访问 /action 或 /movie 时,它的显示效果很好。但是当我访问不在数据库中的 /test 或 /something slug 之类的东西时,它会显示这样的错误
当我访问 /test 时显示“404 Page not found”之类的错误而不是上图的错误时如何解决?
这是我的代码
cdetail.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Movrev - Movie Review for all genre</title>
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<meta content="Consulting Website Template Free Download" name="keywords">
<meta content="Consulting Website Template Free Download" name="description">
<!-- Favicon -->
<link href="img/favicon.ico" rel="icon">
<!-- Google Font -->
<link href="https://fonts.googleapis.com/css2?family=Lato&family=Oswald:wght@200;300;400&display=swap" rel="stylesheet">
<!-- CSS Libraries -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.0/css/all.min.css" rel="stylesheet">
<link href="{{ asset('lib/animate/animate.min.css" rel="stylesheet')}}">
<link href="{{ asset('lib/owlcarousel/assets/owl.carousel.min.css')}}" rel="stylesheet">
<!-- Template Stylesheet -->
<link href="{{ asset('css/style.css')}}" rel="stylesheet">
</head>
<body class="page">
<!-- Nav Bar Start -->
<div class="navbar navbar-expand-lg bg-dark navbar-dark">
<div class="container-fluid">
<a href="index.html" class="navbar-brand">Movie Review</a>
<button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbarCollapse">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-between" id="navbarCollapse">
<div class="navbar-nav ml-auto">
<a href="{{route('welcome')}}" class="nav-item nav-link">Home</a>
<div class="nav-item dropdown">
<a href="#" class="nav-link dropdown-toggle active" data-toggle="dropdown">Category</a>
<div class="dropdown-menu">
<?php $i=0 ?>
@foreach($cat as $c)
<a href="{{route('cdetail', $cat[$i]['slug'])}}" class="dropdown-item">{{$cat[$i]["name"]}}</a>
<?php $i++ ?>
@endforeach
</div>
</div>
@if (Route::has('login'))
@auth
<a href="{{ url('/home') }}" class="nav-item nav-link">Profile</a>
@else
<a href="{{ route('login') }}" class="nav-item nav-link">Login</a>
@if (Route::has('register'))
<a href="{{ route('register') }}" class="nav-item nav-link" >Register</a>
@endif
@endauth
</div>
@endif
</div>
</div>
</div>
</div>
<!-- Nav Bar End -->
<!-- Blog Start -->
<div class="blog blog-page mt-125">
<div class="container">
<div class="section-header">
<p>All reviews from category</p>
<h2>{{$category[0]["name"]}}</h2>
</div>
<div class="row">
<?php $i=0 ?>
@foreach($categories as $category)
<div class="col-md-6">
<div class="blog-item">
<div class="blog-img">
<img style="width: 100%;height: 20vw;object-fit: cover;" src="<?php echo asset("uploads/banner/".$categories[$i]["banner"])?>" alt="Blog">
</div>
<div class="blog-content">
<h2 class="blog-title">{{$categories[$i]["title"]}}</h2>
<div class="blog-meta">
<i class="fa fa-list-alt"></i>
<a href="">{{$categories[$i]["category"]["name"]}}</a>
<i class="fa fa-calendar-alt"></i>
<p>{{$categories[$i]["created_at"]->format('l, j F Y')}}</p>
</div>
<div class="blog-text">
<p>
{{str_limit($categories[$i]["content"], 100 ," ...")}}
</p>
<a class="btn" href="{{ route('detail', [$categories[$i]['category']['slug'], $categories[$i]['slug']]) }}">Read More</a>
</div>
</div>
</div>
</div>
<?php $i++ ?>
@endforeach
</div>
</div>
</div>
<!-- Blog End -->
</body>
</html>
欢迎控制器.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Article;
use App\Category;
class WelcomeController extends Controller
{
public function welcome(){
$articles = Article::orderBy('created_at', "desc")->paginate(5);
$cat = Category::get();
return view('welcome', compact('articles','cat'));
}
public function cdetail($slug_category){
$categories = Article::with('category')->whereHas('category', function($q) use($slug_category) {
$q->where('slug', '=', $slug_category);})
->get();
$category = Category::where('slug', '=', $slug_category)->get();
$cat = Category::get();
return view ('cdetail', compact('categories','category','cat'));
}
public function detail($slug_category, $slug){
$cat = Category::get();
$article = $categories = Article::with('category')->whereHas('category', function($q) use($slug_category) {
$q->where('slug', '=', $slug_category);})
->where('slug', $slug)
->first();
return view ('detail', compact('article','cat'));
}
}
欢迎.blade.php
<html lang="en">
<head>
<meta charset="utf-8">
<title>Movrev - Movie Review for all genre</title>
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<meta content="Consulting Website Template Free Download" name="keywords">
<meta content="Consulting Website Template Free Download" name="description">
<!-- Favicon -->
<link href="img/favicon.ico" rel="icon">
<!-- Google Font -->
<link href="https://fonts.googleapis.com/css2?family=Lato&family=Oswald:wght@200;300;400&display=swap" rel="stylesheet">
<!-- CSS Libraries -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.0/css/all.min.css" rel="stylesheet">
<link href="{{ asset('lib/animate/animate.min.css" rel="stylesheet')}}">
<link href="{{ asset('lib/owlcarousel/assets/owl.carousel.min.css')}}" rel="stylesheet">
<!-- Template Stylesheet -->
<link href="{{ asset('css/style.css')}}" rel="stylesheet">
</head>
<body>
<!-- Nav Bar Start -->
<div class="navbar navbar-expand-lg bg-dark navbar-dark">
<div class="container-fluid">
<a href="index.html" class="navbar-brand">Movie Review</a>
<button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbarCollapse">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-between" id="navbarCollapse">
<div class="navbar-nav ml-auto">
<a href="{{route('welcome')}}" class="nav-item nav-link active">Home</a>
<div class="nav-item dropdown">
<a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">Category</a>
<div class="dropdown-menu">
<?php $i=0 ?>
@foreach($cat as $c)
<a href="{{route('cdetail', $cat[$i]['slug'])}}" class="dropdown-item">{{$cat[$i]["name"]}}</a>
<?php $i++ ?>
@endforeach
</div>
</div>
@if (Route::has('login'))
@auth
<a href="{{ url('/home') }}" class="nav-item nav-link">Profile</a>
@else
<a href="{{ route('login') }}" class="nav-item nav-link">Login</a>
@if (Route::has('register'))
<a href="{{ route('register') }}" class="nav-item nav-link" >Register</a>
@endif
@endauth
</div>
@endif
</div>
</div>
</div>
</div>
<!-- Nav Bar End -->
<!-- Carousel Start -->
<div class="carousel">
<div class="container-fluid">
<div class="owl-carousel">
<div class="carousel-item">
<div class="carousel-img">
<img src="img/knives-out.png" alt="Image">
</div>
<div class="carousel-text">
<h1>Knives Out</h1>
<p>
Knives Out is a 2019 American mystery film written and directed by Rian Johnson, and produced by Johnson and Ram Bergman. It follows a master detective investigating the death of the patriarch of a wealthy, dysfunctional family.
</p>
<div class="carousel-btn">
<a class="btn btn-play" data-toggle="modal" data-src="https://www.youtube.com/embed/qOg3AoRc4nI" data-target="#videoModal"><i class="fa fa-play"></i>Watch Trailer</a>
</div>
</div>
</div>
<div class="carousel-item">
<div class="carousel-img">
<img src="img/the-gifted.jpg" alt="Image">
</div>
<div class="carousel-text">
<h1>The Gifted</h1>
<p>
Pawaret "Pang" Sermrittirong (Korapat Kirdpan) is a struggling, ordinary 10th grade student in Ritdha High School. Here, a classification system is strictly implemented wherein students are divided into classes based on academic excellence. Pang is in Class VIII, the lowest level, and has no hopes of rising up the hierarchy.
</p>
<div class="carousel-btn">
<a class="btn btn-play" data-toggle="modal" data-src="https://www.youtube.com/embed/1c3FJXZBmIk" data-target="#videoModal"><i class="fa fa-play"></i>Watch Trailer</a>
</div>
</div>
</div>
<div class="carousel-item">
<div class="carousel-img">
<img src="img/start-up.jpg" alt="Image">
</div>
<div class="carousel-text">
<h1>Start Up</h1>
<p>
Set in South Korea's fictional Silicon Valley called Sandbox, Start-Up tells the story of people in the world of startup companies.
Seo Dal-mi (Bae Suzy) is a bright and ambitious young woman who dreams of becoming Korea’s Steve Jobs. Dal-mi doesn’t have a fancy background but she’s passionate about her work. She has bright energy and is a person of great vitality, having experience in a wide range of part-time jobs.
</p>
<div class="carousel-btn">
<a class="btn btn-play" data-toggle="modal" data-src="https://www.youtube.com/embed/BemKyzbLDDc" data-target="#videoModal"><i class="fa fa-play"></i>Watch Trailer</a>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Carousel End -->
<!-- Video Modal Start-->
<div class="modal fade" id="videoModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<!-- 16:9 aspect ratio -->
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="" id="video" allowscriptaccess="always" allow="autoplay"></iframe>
</div>
</div>
</div>
</div>
</div>
<!-- Video Modal End -->
<!-- Blog Start -->
<div class="blog">
<div class="container">
<div class="section-header">
<h2>Latest Movie Review</h2>
</div>
<div class="owl-carousel blog-carousel">
@foreach($articles as $a)
<div class="blog-item">
<div class="blog-img">
<img style="width: 100%;height: 20vw;object-fit: cover;" src="<?php echo asset("uploads/banner/$a->banner")?>" alt="Blog">
</div>
<div class="blog-content">
<h2 class="blog-title">{{$a->title}}</h2>
<div class="blog-meta">
<i class="fa fa-list-alt"></i>
<a href="{{route('cdetail', $a->category->slug)}}">{{$a->category->name}}</a>
<i class="fa fa-calendar-alt"></i>
<p><?php echo $a->created_at->format('l, j F Y')?></p>
</div>
<div class="blog-text">
<p>
{{str_limit($a->content, 100 ," ...")}}
</p>
<a class="btn" href="{{ route('detail', [$a->category->slug, $a->slug])}}">Read More</a>
</div>
</div>
</div>
@endforeach
</div>
</div>
</div>
<!-- Blog End -->
</body>
</html>
detail.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Movrev - Movie Review for all genre</title>
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<meta content="Consulting Website Template Free Download" name="keywords">
<meta content="Consulting Website Template Free Download" name="description">
<!-- Favicon -->
<link href="img/favicon.ico" rel="icon">
<!-- Google Font -->
<link href="https://fonts.googleapis.com/css2?family=Lato&family=Oswald:wght@200;300;400&display=swap" rel="stylesheet">
<!-- CSS Libraries -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.0/css/all.min.css" rel="stylesheet">
<link href="{{ asset('lib/animate/animate.min.css" rel="stylesheet')}}">
<link href="{{ asset('lib/owlcarousel/assets/owl.carousel.min.css')}}" rel="stylesheet">
<!-- Template Stylesheet -->
<link href="{{ asset('css/style.css')}}" rel="stylesheet">
</head>
<body class="page">
<!-- Nav Bar Start -->
<div class="navbar navbar-expand-lg bg-dark navbar-dark">
<div class="container-fluid">
<a href="index.html" class="navbar-brand">Movie Review</a>
<button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbarCollapse">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-between" id="navbarCollapse">
<div class="navbar-nav ml-auto">
<a href="{{route('welcome')}}" class="nav-item nav-link">Home</a>
<div class="nav-item dropdown">
<a href="#" class="nav-link dropdown-toggle active" data-toggle="dropdown">Category</a>
<div class="dropdown-menu">
<?php $i=0 ?>
@foreach($cat as $c)
<a href="{{route('cdetail', $cat[$i]['slug'])}}" class="dropdown-item">{{$cat[$i]["name"]}}</a>
<?php $i++ ?>
@endforeach
</div>
</div>
@if (Route::has('login'))
@auth
<a href="{{ url('/home') }}" class="nav-item nav-link">Profile</a>
@else
<a href="{{ route('login') }}" class="nav-item nav-link">Login</a>
@if (Route::has('register'))
<a href="{{ route('register') }}" class="nav-item nav-link" >Register</a>
@endif
@endauth
</div>
@endif
</div>
</div>
</div>
</div>
<!-- Nav Bar End -->
<!-- Single Page Start -->
<div class="mt-125">
<div class="container">
<div class="section-header">
<p>Category: <a href="{{route('cdetail', $article->category->slug)}}">{{$article->category->name}}</a></p>
<h2>{{$article->title}}</h2>
</div>
<div class="row">
<div class="col-12" style="text-align:center;">
<img style="height: 500px;
display: inline-block;margin:0 0 25px 0" src="<?php echo asset("uploads/banner/$article->banner")?>" alt="Image">
</div>
<p>
{{$article->content}}
</p>
</div>
</div>
</div>
<!-- Single Page End -->
</body>
</html>
解决方案
这不是因为您的数据库中有参数(电影/动作),而是因为您的资源/视图中有刀片文件。
当您尝试使用/test
or/something
时,它会在您的视图中查找刀片文件为test.blade.php
orsomething.blade.php
并且失败会显示错误404 not found。
推荐阅读
- video - 希望播放非编码视频上传,然后通过 FFMPEG 从选择点导出 gif
- python - 我在 python 中的循环语句时出错
- android - Gradle/Android Studio 在构建期间如何为每个依赖项创建 ash 文件夹?
- javascript - 向圆环图添加文本 - ChartJS
- oracle - 我在编程中不断遇到这个问题
- c++ - C仅使用指针从多维数组中提取特定列
- python - 根据总值中的值比例拆分熊猫列
- html - 使 div 成为链接,两个代码之间的差异
- java - 修改 switch 语句内的变量以在 switch 外部使用
- java - 如何在同一个正则表达式中使用捕获的组?