Merhaba, ileteceğim kodlar yardımı ile postmanda get, post gibi isteklerimi test etmek istiyorum ancak 404 dönüyor. Kontrol ettiğimde hatamı göremedim. Sizlerden destek rica edebilir miyim?
api.php
use App\Http\Controllers\NewsController;
use Illuminate\Support\Facades\Route;
Route::resource('news', NewsController::class);
NewsController.php
namespace App\Http\Controllers;
use App\Models\News;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class NewsController extends Controller
{
public function index()
{
$news = News::all();
return response()->json($news);
}
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => 'required|string|max:255',
'content' => 'required|string',
]);
if ($validator->fails()) {
return response()->json(['error' => $validator->errors()], 400);
}
$newsItem = News::create([
'title' => $request->input('title'),
'content' => $request->input('content'),
]);
return response()->json($newsItem, 201);
}
public function show($id)
{
$newsItem = News::find($id);
if (!$newsItem) {
return response()->json(['error' => 'Not found news.'], 404);
}
return response()->json($newsItem);
}
public function update(Request $request, $id)
{
$validator = Validator::make($request->all(), [
'title' => 'required|string|max:255',
'content' => 'required|string',
]);
if ($validator->fails()) {
return response()->json(['error' => $validator->errors()], 400);
}
$newsItem = News::find($id);
if (!$newsItem) {
return response()->json(['error' => 'Not found news.'], 404);
}
$newsItem->update([
'title' => $request->input('title'),
'content' => $request->input('content'),
]);
return response()->json($newsItem);
}
public function destroy($id)
{
$newsItem = News::find($id);
if (!$newsItem) {
return response()->json(['error' => 'Not found news.'], 404);
}
$newsItem->delete();
return response()->json(['message' => 'News successfully deleted.']);
}
}
``
News.php
``
use App\Models\News;
public function index()
{
$news = News::all();
return response()->json($news);
}
public function store(Request $request)
{
$newsItem = News::create([
'title' => $request->input('title'),
'content' => $request->input('content'),
]);
return response()->json($newsItem, 201);
}
``