Php Laravel中的资源控制器概念
在学习laravel的过程中,当我试图通过在URL中键入posts/edit(该文件位于resource/views/posts)来访问我的edit.blade.php页面时,我无法理解这一点 它正在调用show方法并在该页面上打印“show”,如果我键入posts/posts/edit,就会显示edit.blade.php(如下所述)。请告诉我我做错了什么 edit.blade.phpPhp Laravel中的资源控制器概念,php,laravel,eloquent,laravel-7,Php,Laravel,Eloquent,Laravel 7,在学习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
@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
你的路线是这样的:
GET posts/{post}/edit EDIT
GET posts/{post} SHOW
因此URIposts/edit
与显示路线匹配:
posts/edit posts/{post}
posts/edit
URIposts/posts/edit
与编辑路径匹配:
posts/posts/edit posts/{post}/edit
posts/posts /edit
这是预期的,以及路线的设置方式
首先,您可以这样更改控制器
<?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('/');
}
}
然后在view/posts
文件夹中创建show.blade.php
,并将此代码放入show.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
@extends('main')
@section('content')
<h1>Show Post</h1>
<h1>{{ $post->title }}</h1>
<p>{{ $post->body }}</p>
@endsection
@extends('main'))
@节(“内容”)
展示柱
{{$post->title}
{{$post->body}
@端部
您会遇到哪个错误?请参阅此处的官方资源控制器文档,您不使用模型路由绑定,这可能会解决您的问题。只需执行posts$post
即可,而不是将$id
发送到控制器(每个方法的参数)。然后,您就可以准备好$post
变量,并且不需要手动获取它。另外,请注意,您的模型违反了Laravel命名约定。它应该是Post
(首字母大写,单数),而不是posts
(全小写,复数)。如果您刚开始使用这个模型和那个控制器,您最好删除这两个,然后使用php artisan make:model Post-rc重新生成它们(使用已定义的资源丰富的控制器生成模型Post
)。但是我要使用的方法是PUT(更新),如果方法不同,它如何仍然调用show方法。请指导我show是什么?…您实际上是说您正在键入URL,这意味着它是一个GET请求
@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
@extends('main')
@section('content')
<h1>Show Post</h1>
<h1>{{ $post->title }}</h1>
<p>{{ $post->body }}</p>
@endsection