Laravel 5 Symfony\Component\HttpKernel\Exception\NotFoundHttpException: POST http://localhost/myproject/public/leads
Laravel 5 Symfony\Component\HttpKernel\Exception\NotFoundHttpException: POST http://localhost/myproject/public/leads
我使用的是Laravel 5.8,并且有一个像这样的测试方法:\n
public function test_store() { $attribute= ['lead_name'=>'test_name','lead_family'=>'test_family']; $this->post('/leads', $attribute); $this->assertDatabaseHas('leads', $attribute); }
\n以及这样的控制器:\n
class LeadController extends Controller { public function store(Request $request) { Lead::create($request->all()); } }
\n和路由:\n
Route::resource('/leads', 'App\Http\Controller\LeadController');
\n当我运行phpunit时,显示了以下错误:\n
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: POST http://localhost/myproject/public/leads C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithExceptionHandling.php:118 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:326 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:120 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:375 C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:197 C:\wamp64\www\myproject\tests\Feature\Customer\LeadTest.php:38
\n我使用Postman测试了`leads`的URL,它正常工作。\n我阅读了这篇帖子:https://stackoverflow.com/questions/23593892/symfony-component-httpkernel-exception-notfoundhttpexception-laravel
问题原因:在Laravel 5中,出现了Symfony\Component\HttpKernel\Exception\NotFoundHttpException异常,原因是没有正确配置路由或者LeadController没有放置在正确的目录中。
解决方法:
1. 确保路由正确配置,可以按照以下方式设置路由:
Route::resource('leads', 'LeadController');
2. 确保LeadController文件放置在正确的目录中,即`app/Http/Controller`目录下。
3. 可以阅读Laravel - Route::resource vs Route::controller中的内容,了解更多有关路由的信息。
4. 确保路由正确设置,可以使用postman等工具进行测试,验证路由是否正确。
5. 如果有权限,可以尝试使用teamviewer等远程工具进行调试。
6. 打开控制台,运行以下命令进行清理缓存和重新加载路由:
php artisan cache:clear php artisan route:clear composer dumpautoload
运行以上命令后,再次检查是否仍然出现该异常。