【发布时间】:2021-06-30 02:31:03
【问题描述】:
我正在尝试返回现有视图,但出现“404 not found”。我是初学者,我还不太了解 Laravel。任何想法为什么显示错误 404 而不是显示页面?我已经尝试过“使用视图”,但仍然没有任何效果。 PostsController.php:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
use Cviebrock\EloquentSluggable\Services\SlugService;
class PostsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('blog.index')
->with('posts', Post::orderBy('updated_at', 'DESC')->get());
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('blog.create');
}
/**
* Display the specified resource.
*
* @param string $slug
* @return \Illuminate\Http\Response
*/
//NOT WORKING THIS
public function show($slug)
{
return view('blog.show')->with('post', Post::where('slug', $slug)->first());
}
...
show.blade.php:
@extends('layouts.app')
@section('content')
<div class="w-full m-auto text-center">
<div class="py-15 border-b border-gray-200 w-full">
<h1 class="text-4xl uppercase">
{{ $post->title }}
</h1>
</div>
</div>
<div class="px-5">
<span class="text-gray-500">
By <span class="font-bold italic text-gray-800">{{$post->user->name}}</span>
, Uploaded on {{date('jS M Y', strtotime($post->updated_at))}}
</span>
<p class="text-xl text-gray-700 pt-8 pb-10 leading-8 font-light">
{{ $post->description }}
</p>
</div>
@endsection
这些是我的路线。 web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PagesController;
use App\Http\Controllers\PostsController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', [PagesController::class, 'index']);
Route::resource('/mods', PostsController::class);
Auth::routes();
Route::get('/home', [\App\Http\Controllers\HomeController::class, 'index'])->name('home');
【问题讨论】:
-
发布您的路线
-
@sta 我发布了它们
-
确保 show.blade.php 位于正确的文件夹中 (../resources/views/blog/show.blade.php)
-
它在那里,是的
-
你需要定义路由