问题
Hello I try to create post with Laravel, but I have this error :
Class App\Http\Controllers\PostConstroller does not exist
Route:
Route::get('/', function () {
return view('welcome');
});
Auth::routes(['verify' => true]);
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/profile/{user}', 'ProfileController@show')->name('profile.show');
Route::get('/posts/create', 'PostConstroller@create')->name('posts.create');
Route::post('/posts', 'PostConstroller@store')->name('posts.store');
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function create()
{
return view('posts.create');
}
}
Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
}
and I can do for leave this error?
回答1:
Hey you didn't notice you have spelling mistake in your import it should be PostController
not PostConstroller
Change this
App\Http\Controllers\PostConstroller
to
App\Http\Controllers\PostController
and yes same issue is with your routes:
Route::get('/posts/create', 'PostConstroller@create')->name('posts.create');
Route::post('/posts', 'PostConstroller@store')->name('posts.store');
Change your routes to:
Route::get('/posts/create', 'PostController@create')->name('posts.create');
Route::post('/posts', 'PostController@store')->name('posts.store');
Thanks
来源:https://stackoverflow.com/questions/57019062/class-app-http-controllers-postconstroller-does-not-exist