首先让我说“我知道这个问题会被问到很多。”当我说没有什么对我有用时,请相信我。
我创建了一个名为PostController
的控制器。这是我博客的控制器。当我导航到我的博客时,我得到以下错误Class App\Http\Controllers\PostController does not exist
,即使它确实存在。控制器名为PostController.php
。这是路线Route::get('blog','PostController@index');
的样子。我已经读过,运行一些composer
命令会有所帮助,但没有一个能帮助我。 composer dumpautoload
以及composer update
。我在这里错过了一些步骤吗?有人遇到过类似的问题吗?如果需要更多信息,请告诉我。
修改 以下是顶部的命名空间。
use App\Http\Controllers;
use App\Posts;
use App\User;
use App\Http\Controllers\Controller;
use App\Http\Requests\PostFormRequest;
use Illuminate\Http\Request;
这是整个控制器。
<?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);
}
}
感谢。
答案 0 :(得分:3)
打开App \ Provider \ RouteServiceProvider.php 并添加 这条线
protected $namespace = 'App\Http\Controllers';
在此下方
public const HOME = '/home';
您的错误将消失。
答案 1 :(得分:1)
如果composer dumpautoload
没有帮助,请检查namespace
中是否有正确的PostController.php
声明,并在班级名称/路线声明中仔细检查拼写错误。
如果失败,请检查composer.json
是否有自动加载配置,它应该是这样的:
"autoload": {
"psr-4": {
"App\\": "app/"
}
},
作为附注,您可以使用以下内容:
Route::get('blog',PostController::class . '@index');
或
Route::get('blog',\App\Http\Controllers\PostController::class . '@index');
有了这个任何体面的IDE,如果它找不到文件那么应该给出某种警告
修改强>:
您的文件应该有这样的一行
namespace App\Http\Controllers;
如果你正在使用php7严格模式,请在<?php
或<?php declare(strict_types = 1);
之后立即开始文件的初始化
答案 2 :(得分:1)
打开App \ Providers \ RouteServiceProvider.php并取消注释行:
protected $namespace = 'App\\Http\\Controllers';
谢谢,这个方法对我有帮助!
答案 3 :(得分:0)
如果您使用laravel 8. *,则打开App-> Providers> RouteServiceProvider.php并取消注释此行:
受保护的$ namespace ='App \ Http \ Controllers';
答案 4 :(得分:0)
这发生在我从 Laravel 7.x 升级到 8.x 的过程中。我的解决方法是更改我的路由:
Route::resource('posts', 'PostController');
到:
Route::resource('posts', \App\Http\Controllers\PostController::class);
或者如果您导入 PostController 类:
Route::resource('posts', PostController::class);
从 Laravel 7.x 到 8.x 的升级指南中解释了这一变化的原因: https://laravel.com/docs/8.x/upgrade#routing
它解释说它 8.x 支持允许路由声明使用标准的 PHP 可调用语法。另一种解决方案是其他人提到的,并将 protected $namespace = 'App\Http\Controllers'; 添加到 RouteServiceProvider。