访问资源/视图中的
error 404 not found
时,我收到 home.blade.php
这是名为
postsController
的控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;
class postsController extends Controller
{
/**
*
*/
public function index(): View
{
//$posts = DB::table('posts')->get();
return view('home.index', ['users' => $posts]);
}
}
其他文件看起来是
web.php 中的路由
Route::get('/home', [PostsController::class, 'index'])->name('home.index');
home.blade.php
中的部分代码用于显示所有帖子
<h1>Posts</h1>
<ul>
@foreach ($posts as $post)
<li>
{{ $post->title }} - {{ $post->body }}
</li>
@endforeach
</ul>
上面的代码有什么问题?如何正确书写路线以到达所需地点?即
home.blade.php
blade.php
仅用作模板源代码的文件扩展名,它永远不应该在您的代码中真正使用,也不应该被您的用户使用。
home.blade.php
不是一条路线,它只是 Blade
的模板文件
resources/views
只是一个“源代码”文件夹,永远不应该从浏览器直接访问它
在浏览器中,您会执行
example.com/home
而不是 example.com/home.blade.php
在你的控制器中你可以像这样调用视图
// 'home' is the name of the view
return view('home', ['posts' => $posts]);
在不同的控制器方法中,您可能想做这样的事情
// 'home.index' is the name of the route
return redirect()->route('home.index');