phplaravel

Laravel API Not Found


I'm trying to test my API and for this matter I don't need authentication for my API all I want to do is to share my published posts with API but I get 404 page.

Code

controller

<?php

namespace App\Http\Controllers\API;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Post;

class PostController extends Controller
{
    public function index(){
        return Post::orderby('id', 'desc')->where('status', '=', '1')->get();
    }

    public function single($slug){
        return Post::where('slug', $slug)->where('status', '=', '1')->firstOrFail();
    }
}

api.php (routes folder)

Route::get('posts', 'API\PostController@index');
Route::get('posts/{slug}', 'API\PostController@single');

I tried to access my api posts with url like: http://newapp.test/api/posts and it returns 404 error.

Any idea?

Update

api.php

<?php

use Illuminate\Http\Request;

// Route::middleware('auth:api')->get('/user', function (Request $request) {
//     return $request->user();
// });

Route::get('posts', 'API\PostController@index');
Route::get('posts/{slug}', 'API\PostController@single');

Solution

  • Leave all things as it is and RUN Command

    php artisan route:clear