【问题标题】:Resource controller Concept In LaravelLaravel 中的资源控制器概念
【发布时间】:2020-12-09 12:02:01
【问题描述】:

当我正在学习 laravel 时,我无法理解当我尝试通过在 URL 中键入帖子/编辑来访问我的 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');

【问题讨论】:

  • 您遇到了哪个错误?在此处查看官方资源控制器文档laravel.com/docs/7.x/controllers#resource-controllers
  • 您不使用模型路由绑定,这可能会解决您的问题。不要将$id 发送到您的控制器(每个方法的参数),只需执行posts $post。然后你就准备好了$post 变量,你不需要手动获取它。
  • 另外,旁注 - 你的模型违反了 Laravel 命名约定。它应该是Post(第一个字母大写,单数)而不是posts(全小写,复数)。
  • 如果你刚开始使用这个模型和那个控制器,你最好删除它们,然后用php artisan make:model Post -rc重新生成它们(生成模型Post,并使用已经定义的资源丰富的控制器)。

标签: php laravel eloquent laravel-7


【解决方案1】:

首先你像这样改变你的控制器

<?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('/');

    }
}

在您的edit.blade.php 中添加$post-&gt;id 以形成route

@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

然后你在view/posts文件夹中创建show.blade.php并将这段代码放到show.blade.php

@extends('main')
@section('content')
<h1>Show Post</h1>
<h1>{{ $post->title }}</h1> 
<p>{{ $post->body }}</p> 
@endsection

【讨论】:

    【解决方案2】:

    你的路线是这样的:

    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

    【讨论】:

    • 但是我要使用的方法是PUT(更新),如果方法不同,它怎么仍然调用show方法。请指导我
    • 显示的内容如何? ...您实际上是在说您正在输入 URL,这意味着它是一个 GET 请求
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    相关资源
    最近更新 更多