1

I have installed jwt authentication & I have created a controller i.e., AuthController Inside Api Directory. I have defined the in routes/api.php as:

Route::group(['prefix'=>'v1', 'namespace' => 'Api'],function($app){
    Route::get('/test', function(){
        return "HEllo";
    });
    Route::get('test',              'AuthController@test');
});

When I hit the url as: http://localhost:8000/api/v1/test then I am getting error as Class Cotrollers\Api\AuthController does not exist.

AuthController

<?php

namespace App\Http\Controllers;

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

class AuthController extends Controller
{


    public function test() {
        return "Hello";
    }
}

RouteServiceProvider.php:

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

5 Answers 5

4

Uncomment the ->namespace($this->namespace) line.

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

3 Comments

Route::prefix('api') // ->middleware('api') // ->namespace($this->namespace) ->group(base_path('routes/api.php'));
Yes. Please un comment "->namespace" line and check.
I've tidied up the question to include your php, and the answer to include the answer. In future you shouldn't post answers asking for clarification because it makes it very difficult to follow, and the necessary information isn't in the question itself - making it harder for others to help the OP.
2

In your Route::group statement you have defined the namespace of the route group as 'Api'.

But the AuthController resides in the App\Http\Controllers namespace, and not the Api namespace.

To fix this add an Api namespace in your App\Http\Controllers and refer it there (best practice is creating a directory in the Controllers directory named Api so the directory structure follows the namespace):

AuthController.php

namespace App\Http\Controllers\Api;

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

class AuthController extends Controller
{
    public function test() {
        return "Hello";
    }
}

2 Comments

Now I am getting Class Api\AuthController does not exist error
Try running composer dump-autoload to update the class reference in composer's autoloader
2

Here you need to make changes to,

App\Providers\RouteServiceProvider.php

In the RouteServiceProvider.php add

protected $namespace = 'Path\To\Controllers';

Like:

protected $namespace = 'App\Http\Controllers';

Thats it! Please let me know if this solved your problem.

Comments

1

Change the Auth controller namespace definition to:

namespace App\Http\Controllers\Api;

1 Comment

has no effect , getting same errror Class Api\AuthController does not exist
0
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;

// you need to use your controller top of the api.php file

    Route::group([
        'namespace' => 'Customers', //namespace App\Http\Controllers\Customers;
        'middleware' => 'auth:api', // this is for check user is logged in or authenticated user
        'prefix' => 'customers' // you can use custom prefix for your rote {{host}}/api/customers/
    
    ], function ($router) {
        // add and delete customer groups
        Route::get('/', [CustomerController::class, 'index']); // {{host}}/api/customers/  this is called to index method in CustomerController.php
        Route::post('/create', [CustomerController::class, 'create']); // {{host}}/api/customers/create this is called to create method in CustomerController.php
        Route::post('/show/{id}', [CustomerController::class, 'show']); // {{host}}/api/customers/show/10 this is called to show method in CustomerController.php parsing id to get single data
        Route::post('/delete/{id}', [CustomerController::class, 'delete']); // {{host}}/api/customers/delete/10 this is called to delete method in CustomerController.php for delete single data
    });

Comments

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.