Laravel项目中获取类App \ Http \ Controllers \ PostController不存在错误

时间:2018-09-21 13:18:54

标签: php laravel

尝试创建新帖子时遇到这些错误:

  

Route.php第280行中的2/2 ReflectionException:   App \ Http \ Controllers \ PostController类不存在

     PostController.php第70行中的

1/2 FatalThrowableError:   解析错误:语法错误,意外的“会话”(T_STRING)

一切正常,直到我在store()方法中添加以下代码行:

$post->tags()->sync($request->tags, false);

这是我的控制人

<?php
 namespace App\Http\Controllers;

 use App\Category;
 use App\Http\Controllers\Controller;
 use App\Http\Requests;
 use App\Post;
 use App\Tag;
 use Illuminate\Http\Request;
 use Session;

 class PostController extends Controller
 {
   public function __construct()
   {
      $this->middleware('auth');
   }

   public function index()
   {
      $posts = Post::orderBy('id', 'desc')->paginate(10);
      return view('posts.index')->withPosts($posts);
   }

   public function create()
   {
      $categories = Category::pluck('name', 'id');
      $tags = Tag::pluck('name', 'id');
      return view('posts.create', compact('categories', 'tags'));
   }

   public function store(Request $request)
   {
      $this->validate($request, [
            'title' => 'required|max:255',
            'slug'  => 
            'required|alpha_dash|min:5|max:255|unique:posts,slug',
            'category_id' => 'required|integer',
            'body'  => 'required'
        ]);

       $post = new Post;

       $post->title = $request->title;
       $post->slug = $request->slug;
       $post->category_id = $request->category_id;
       $post->body = $request->body;

       $post->save();

       $post->tags()->sync($request->tags, false);

       Session::flash('success', 'The blog post was successfully save!');

       return redirect()->route('posts.show', $post->id);
    }

  public function show($id)
  {
       $post = Post::findOrFail($id);
       return view('posts.show', compact('post'));
  }


  public function edit($id)
  {
      $post = Post::findOrFail($id);
      $categories = Category::pluck('name', 'id');
      return view('posts.edit', compact('post', 'categories'));
  }

  public function update(Request $request, $id)
  {
      $post = Post::find($id);
      if ($request->input('slug') == $post->slug) {
        $this->validate($request, array(
            'title' => 'required|max:255',
            'category_id' => 'required|integer',
            'body'  => 'required'
        ));
      } else {
        $this->validate($request, array(
            'title' => 'required|max:255',
            'slug'  => 
            'required|alpha_dash|min:5|max:255|unique:posts,slug',
            'category_id' => 'required|integer',
            'body'  => 'required'
        ));
    }

          $post = Post::find($id);

          $post->title = $request->input('title');
          $post->slug = $request->input('slug');
          $post->category_id = $request->input('category_id');
          $post->body = $request->input('body');

          $post->save();

         Session::flash('success', 'This post was successfully saved.');

         redirect with flash data to posts.show
         return redirect()->route('posts.show', $post->id);
     }

     public function destroy($id)
     {
        $post = Post::find($id);

        $post->delete();

        Session::flash('success', 'The post was successfully deleted.');
        return redirect()->route('posts.index');
     }
   }

1 个答案:

答案 0 :(得分:1)

您有一个看不见的角色:

$post->tags()->sync($request->tags, false);<feff>

如果您的编辑器可以查看不可打印的字符,则可能会显示类似这样的字符。