Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller not found #27

Closed
GoldraK opened this issue Feb 14, 2021 · 3 comments
Closed

Controller not found #27

GoldraK opened this issue Feb 14, 2021 · 3 comments

Comments

@GoldraK
Copy link

GoldraK commented Feb 14, 2021

First of all, thank you for the time and the package.

I need to customize some of the create and update functions so I have created a controller for the resource as the documentation says

php artisan jsonapi:controller Api/V1/BuildingController

In routes add the buildings resource

JsonApiRoute::server('v1')
    ->prefix('v1')
    ->namespace('Api\V1')
    ->resources(function ($server) {
        $server->resource('buildings');
    });

But when I go to see the routes, it say me the Controller not exists

php artisan r:l --columns=URI,name
Cannot load Xdebug - it was already loaded

   Illuminate\Contracts\Container\BindingResolutionException 

  Target class [Api\V1\BuildingController] does not exist.

  at vendor/laravel/framework/src/Illuminate/Container/Container.php:832
    828▕ 
    829▕         try {
    830▕             $reflector = new ReflectionClass($concrete);
    831▕         } catch (ReflectionException $e) {
  ➜ 832▕             throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
    833▕         }
    834▕ 
    835▕         // If the type is not instantiable, the developer is attempting to resolve
    836▕         // an abstract type such as an Interface or Abstract Class and there is

  1   [internal]:0
      Illuminate\Foundation\Console\RouteListCommand::Illuminate\Foundation\Console\{closure}(Object(Illuminate\Routing\Route))

      +13 vendor frames 
  15  [internal]:0
      Illuminate\Foundation\Console\RouteListCommand::Illuminate\Foundation\Console\{closure}(Object(Illuminate\Routing\Route))

But is here

image

If I use the JSON:API Controller work perfect

Thanks for your time.

@lindyhopchris
Copy link
Contributor

Hi! So it says this does not exist: Api\V1\BuildingController

As you can see that's missing your App\Http\Controllers namespace before it.

So the answer to this is: it depends whether you're using the $namespace property in your RouteServiceProvider:
https://github.com/laravel/laravel/blob/8.x/app/Providers/RouteServiceProvider.php#L29

It looks like you aren't. So in that case you'd need to do:

JsonApiRoute::server('v1')
    ->prefix('v1')
    ->resources(function ($server) {
        $server->resource('buildings', \App\Http\Controllers\Api\V1\BuildingController::class);
    });

The relevant section of the docs is here:
https://laraveljsonapi.io/docs/1.0/routing/#controllers-and-namespaces

Let me know if that solves it.

@GoldraK
Copy link
Author

GoldraK commented Feb 14, 2021

Thanks for the help.

Now it works perfectly, ignore when I read the paragraph that explains it clearly, sorry for my mistake.

All the best.

@lindyhopchris
Copy link
Contributor

No problem, glad you got it working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants