0

i have a small crud frontend to store information. The frontend controller is called ShowsController. And i want to get alle Shows from the api.

So my routes/web.php contains:

Route::resource('shows', 'ShowsController');

Thats perfect and worked really well.

My routes/api.php contains:

Route::resource('shows', 'ShowsController', ['only' => ['index']]);

The route /api/shows should give me the shows as json. to decide frontend and api i put the ShowsController into Controllers/Api folder

The Controllers/Api/ShowsController contains:

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Show;

class ShowsController extends Controller
{
    public function index(){
        return response()
            ->json(Show::all())->withHeaders([
                'Content-Type' => 'text/json',
                'Access-Control-Allow-Origin' => '*'
            ]);
    }
}

And i also changed the RouteServiceProvider to:

protected function mapApiRoutes()
{
    Route::group([
        'middleware' => 'api',
        'namespace' => 'Api',
        'prefix' => 'api',
    ], function ($router) {
        require base_path('routes/api.php');
    });
}

But the command php artisan route:list gives me an exception:

[ReflectionException]
Class Api\ShowsController does not exist

Why is laravel not finding the defined ShowsController in the api directory?

2
  • have You try : composer dump-autoload? Commented Aug 4, 2017 at 8:27
  • @LorenzoBerti yes, i did before. Commented Aug 4, 2017 at 8:40

1 Answer 1

7

I don't know what version of Laravel you are using, but in 5.4, I have, by default, this method:

protected function mapApiRoutes()
{
    Route::prefix('api')
         ->middleware('api')
         ->namespace($this->namespace)
         ->group(base_path('routes/api.php'));
}

It could work if you replace namespace($this->namespace) by namespace($this->namespace . '\Api').

Sign up to request clarification or add additional context in comments.

1 Comment

thats worked. and now i see why. with my api namespace i cutted App\Http\Controllers so the autoloader is searching my controller at /Api and not at /App/Http/Controllers/Api. thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.