简体   繁体   English

Class App\Http\Controllers\PostController 不存在

[英]Class App\Http\Controllers\PostController does not exist

Let me just start by saying "I know this question gets asked a lot."首先让我说“我知道这个问题经常被问到”。 believe me when i say nothing has worked for me.相信我,当我说什么都对我有用时。

I have created a controller called PostController .我创建了一个名为 PostController 的PostController This is a controller for my blog.这是我博客的 controller。 When I navigate to my blog i get the following error Class App\Http\Controllers\PostController does not exist even though it does exist.当我导航到我的博客时,出现以下错误Class App\Http\Controllers\PostController does not exist ,即使它确实存在。 The controller is called PostController.php . controller 称为PostController.php Here is what the route looks like Route::get('blog','PostController@index');这是路由的样子Route::get('blog','PostController@index'); . . I have read that running some composer commands will help but none of them have helped me.我读过运行一些composer命令会有所帮助,但没有一个对我有帮助。 composer dumpautoload as well as composer update . composer dumpautoload以及composer update Am i missing some step here?我在这里错过了一些步骤吗? Anyone run into a similar problem?有人遇到过类似的问题吗? Please let me know if additional information is needed.如果需要其他信息,请告诉我。

EDIT Here are the namespaces at the top.编辑这是顶部的命名空间。

use App\Http\Controllers;
use App\Posts;
use App\User;
use App\Http\Controllers\Controller;
use App\Http\Requests\PostFormRequest;
use Illuminate\Http\Request;

Here is the whole Controller.这是整个Controller。

<?php 
use App\Http\Controllers;
use App\Posts;
use App\User;
use App\Http\Controllers\Controller;
use App\Http\Requests\PostFormRequest;
use Illuminate\Http\Request;



class PostController extends Controller {

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
    public function index()
    {
      //fetch 5 posts from database which are active and latest
      $posts = Posts::where('active',1)->orderBy('created_at','desc')->paginate(5);
      //page heading
      $title = 'Latest Posts';
      //return home.blade.php template from resources/views folder
      return view('blog/home')->withPosts($posts)->withTitle($title);
    }


/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
    public function create(Request $request)
    {
      // if user can post i.e. user is admin or author
      if($request->user()->can_post())
      {
        return view('blog.create');
      }    
      else 
      {
        return redirect('blog');
      }
    }


/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
    public function store(PostFormRequest $request)
    {
      $post = new Posts();
      $post->title = $request->get('title');
      $post->body = $request->get('body');
      $post->slug = str_slug($post->title);
      $post->author_id = $request->user()->id;
      if($request->has('save'))
      {
        $post->active = 0;
        $message = 'Post saved successfully';            
      }            
      else 
      {
        $post->active = 1;
        $message = 'Post published successfully';
      }
      $post->save();
      return redirect('edit/'.$post->slug)->withMessage($message);
    }


/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
    public function show($slug)
    {
      $post = Posts::where('slug',$slug)->first();
      if(!$post)
      {
         return redirect('/')->withErrors('requested page not found');
      }
      $comments = $post->comments;
      return view('posts.show')->withPost($post)->withComments($comments);
    }


/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
    public function edit(Request $request,$slug)
    {
      $post = Posts::where('slug',$slug)->first();
      if($post && ($request->user()->id == $post->author_id || $request->user()->is_admin())){
          return view('posts.edit')->with('post',$post);
      }
      return redirect('blog')->withErrors('you have not sufficient permissions');
    }


/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
    public function update(Request $request)
    {
      //
      $post_id = $request->input('post_id');
      $post = Posts::find($post_id);
      if($post && ($post->author_id == $request->user()->id || $request->user()->is_admin()))
      {
        $title = $request->input('title');
        $slug = str_slug($title);
        $duplicate = Posts::where('slug',$slug)->first();
        if($duplicate)
        {
          if($duplicate->id != $post_id)
          {
            return redirect('edit/'.$post->slug)->withErrors('Title already exists.')->withInput();
          }
          else 
          {
            $post->slug = $slug;
          }
        }
        $post->title = $title;
        $post->body = $request->input('body');
        if($request->has('save'))
        {
          $post->active = 0;
          $message = 'Post saved successfully';
          $landing = 'edit/'.$post->slug;
        }            
        else {
          $post->active = 1;
          $message = 'Post updated successfully';
          $landing = $post->slug;
        }
        $post->save();
             return redirect($landing)->withMessage($message);
      }
      else
      {
        return redirect('blog')->withErrors('you have not sufficient permissions');
      }
    }


/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return Response
 */
    public function destroy(Request $request, $id)
    {
      //
      $post = Posts::find($id);
      if($post && ($post->author_id == $request->user()->id || $request->user()->is_admin()))
      {
        $post->delete();
        $data['message'] = 'Post deleted Successfully';
      }
      else 
      {
        $data['errors'] = 'Invalid Operation. You have not sufficient permissions';
      }
      return redirect('blog')->with($data);
    }


}

Thanks.谢谢。

Open App\\Provider\\RouteServiceProvider.php and add this line打开 App\\Provider\\RouteServiceProvider.php 并添加这一行

protected $namespace = 'App\Http\Controllers'; 

below this在这个下面

public const HOME = '/home';

and your error will vanish.你的错误就会消失。

If composer dumpautoload is not helping then check if you have proper namespace declaration in PostController.php and double check for typos in class name/route declaration.如果composer dumpautoload是没有帮助,然后检查是否有适当的namespace中声明PostController.php和类名/路由声明错别字双重检查。

If this fails check composer.json for autoload configuration, it should have something like this:如果这失败,请检查composer.json的自动加载配置,它应该是这样的:

"autoload": {
    "psr-4": {
        "App\\": "app/"
    }
},

As a side note you could use something like this:作为旁注,你可以使用这样的东西:

Route::get('blog',PostController::class . '@index');

or

Route::get('blog',\App\Http\Controllers\PostController::class . '@index');

With this any decent IDE should give some kind of a warning if it can't find the file/there's a typo有了这个,如果找不到文件/有错字,任何体面的 IDE 都应该发出某种警告

Edit :编辑

Your file should have a line like this你的文件应该有这样一行

namespace App\Http\Controllers;

At the beggining of the file, right after <?php or <?php declare(strict_types = 1);在文件开始时, <?php declare(strict_types = 1); <?php<?php declare(strict_types = 1); if you're using php7 strict mode如果您使用的是 php7 严格模式

Open App\\Providers\\RouteServiceProvider.php and uncomment row:打开 App\\Providers\\RouteServiceProvider.php 并取消注释行:

protected $namespace = 'App\\Http\\Controllers';

Thank you, this metod help for me!谢谢,这个方法对我有帮助!

if you use laravel 8.* then Open App->Providers>RouteServiceProvider.php and uncomment this line:如果你使用 laravel 8.* 然后打开 App->Providers>RouteServiceProvider.php 并取消注释这一行:

protected $namespace = 'App\\Http\\Controllers';受保护的 $namespace = 'App\\Http\\Controllers';

This happened in my upgrade from Laravel 7.x to 8.x.这发生在我从 Laravel 7.x 升级到 8.x 时。 My fix was to change my routing from:我的解决方法是更改​​我的路由:

Route::resource('posts', 'PostController');

to:到:

Route::resource('posts', \App\Http\Controllers\PostController::class);

or if you import the PostController class:或者如果您导入 PostController 类:

Route::resource('posts', PostController::class);

The reason for this change is explained in the upgrade from Laravel 7.x to 8.x guide: https://laravel.com/docs/8.x/upgrade#routing从 Laravel 7.x 升级到 8.x 指南中解释了此更改的原因: https ://laravel.com/docs/8.x/upgrade#routing

It explains that it 8.x provides support to allow route declarations to use standard PHP callable syntax.它解释说它 8.x 支持允许路由声明使用标准的 PHP 可调用语法。 The alternative solution is what others have mentioned, and add protected $namespace = 'App\\Http\\Controllers';另一种解决方案是其他人提到的,并添加受保护的 $namespace = 'App\\Http\\Controllers'; to RouteServiceProvider .RouteServiceProvider

Simply, You would have to add class into the routes\web.php file as given below:简单地说,您必须将 class 添加到routes\web.php文件中,如下所示:

use App\Http\Controllers\PostController;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM