如何解决Laravel中的资源控制器概念
当我学习laravel时,我无法理解,当我尝试通过在URL中键入posts / edit(该文件位于resource / views / posts中)来访问我的edit.blade.php页面时, 它正在调用show方法并在该页面上打印“ show”,并且如果我键入posts / posts / edit,则将显示edit.blade.php(如下所述)。请在这里指导我我在做什么错
edit.blade.php
@extends('main')
@section('content')
<h1>Update Post</h1>
<form method="POST" action="{{route('posts.update',$post) }}" >
@method('PUT')
@csrf
<input type="text" name="title"><br><br>
<input type="text" name="body"><br><br>
<button type="submit" class="btn btn-primary">Update</button>
</form>
@endsection
PostController.php(资源控制器)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\posts;
use Sessions;
class PostController extends Controller
{
public function create()
{
return view('posts.create');
}
public function store(Request $request)
{
$post = new posts;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect('posts/read');
}
public function show($data)
{
echo "show";
}
public function edit($id)
{
return view('posts.edit');
}
public function update(Request $req,$id)
{
echo posts::where('title',$req->title)
->update(['body'=>$req->body]);
return redirect('/');
}
public function destroy($id)
{
$post = posts::find($id);
$post->delete();
return redirect('/');
}
}
路线:
Route::resource('posts','PostController');
解决方法
您的路线如下:
GET posts/{post}/edit EDIT
GET posts/{post} SHOW
因此URI posts/edit
与SHOW路由匹配:
posts/edit posts/{post}
posts/edit
URI posts/posts/edit
与EDIT路由匹配:
posts/posts/edit posts/{post}/edit
posts/posts /edit
这是预期的,以及如何设置路由。
Laravel 7.x Docs - Controllers - Resource Conrollers
,首先,您要像这样更改控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Post;
use Sessions;
class PostController extends Controller
{
public function create()
{
return view('posts.create');
}
public function store(Request $request)
{
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect('posts/read');
}
public function show($id)
{
$post = Post::find($id);
return view(posts.show,['post' => $post]);
}
public function edit($id)
{
$post = Post::find($id);
return view(posts.edit,['post' => $post]);
}
public function update(Request $request,$id)
{
dd($request->all()); //check before update
}
public function destroy($id)
{
$post = Post::find($id);
$post->delete();
return redirect('/');
}
}
在您的$post->id
中添加route
以形成edit.blade.php
@extends('main')
@section('content')
<h1>Update Post</h1>
<form method="POST" action="{{route('posts.update',$post->id) }}" >
@method('PUT')
@csrf
<input type="text" name="title"><br><br>
<input type="text" name="body"><br><br>
<button type="submit" class="btn btn-primary">Update</button>
</form>
@endsection
然后您在show.blade.php
文件夹中创建view/posts
并将此代码放入show.blade.php
@extends('main')
@section('content')
<h1>Show Post</h1>
<h1>{{ $post->title }}</h1>
<p>{{ $post->body }}</p>
@endsection
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。