Class App\Http\Controllers\API\UserController does not exist
Solution 1
If your controller path is /App/Http/Controllers/API
, you need to adjust it's namespace :
namespace App\Http\Controllers\API;
If your controller path is /App/Http/Controllers
, you need to adjust your routes:
Route::post('login', 'UserController@login');
Solution 2
It could be because you are not calling the right middleware on the user route that directs to that controller. You would have to create a user middleware.
You can do this by navigating to your App\Http\Middleware and add the user middleware with the name UserMiddleware.php and some code to it.
Firstly, you would need to import the following files;
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
Then you create a class called;
class UserMiddleware
Add a handle function to it like so;
public function handle($request, Closure $next)
Inside this function include the following code;
if (Auth::user()->usertype == 'user') { return $next($request); }
Next up, head over to you App\Http\Kernel.php and add the following code at the bottom of the protected $routeMiddleware section;
'user' => \App\Http\Middleware\UserMiddleware::class,
Then go over to your route (API) and include this predefined user middleware to your URLs.
Route::group(['middleware' => 'user'], function () {
Route::post('login', 'API\UserController@login');
Route::post('register', 'API\UserController@register');
Route::post('details', 'API\UserController@details');
});
});
For this to work you would need to have a usertype
field in your users table that is set to user
by default. Your usertype column should look like this;
$table->string('usertype')->nullable()->default('user');
Solution 3
simply just write folder extension in namespace
for example in your case
namespace App\Http\Controllers\API;
And in route you just write
Route::post('register','api\UserController@register');
Related videos on Youtube
Neil Deb
Love to crack Jokes, Attitude of knowledge proliferation Started using Linux Love to do business "" "The more you Invest, the more you get" int a,b; a=1.1*365; //1.1 increasing level of your skill per day cout<<"you increase your skill up to:-"<
Updated on June 04, 2022Comments
-
Neil Deb almost 2 years
I am Having the issue of not getting token in postman as well as the following problem
ReflectionException …\vendor\laravel\framework\src\Illuminate\Container\Container.php790 user controller does not exist
my route file;
Route::post('login', 'API\UserController@login'); Route::post('register', 'API\UserController@register'); Route::group(['middleware' => 'auth:api'], function(){ Route::post('details', 'API\UserController@details'); });
My controller file;
namespace App\Http\Controllers; use App\Http\Controllers\Controller; use App\User; use Illuminate\Support\Facades\Auth; use Validator; use Illuminate\Http\Request; class UserController extends Controller { // public $successStatus = 200; /** * login api * * @return \Illuminate\Http\Response */ public function login(){ if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){ $user = Auth::user(); $success['token'] = $user->createToken('MyApp')-> accessToken; return response()->json(['success' => $success], $this-> successStatus); } else{ return response()->json(['error'=>'Unauthorised'], 401); } } /** * Register api * * @return \Illuminate\Http\Response */ public function register(Request $request) { $validator = Validator::make($request->all(), [ 'name' => 'required', 'email' => 'required|email', 'password' => 'required', 'c_password' => 'required|same:password', ]); if ($validator->fails()) { return response()->json(['error'=>$validator->errors()], 401); } $input = $request->all(); $input['password'] = bcrypt($input['password']); $user = User::create($input); $success['token'] = $user->createToken('MyApp')-> accessToken; $success['name'] = $user->name; return response()->json(['success'=>$success], $this-> successStatus); } /** * details api * * @return \Illuminate\Http\Response */ public function details() { $user = Auth::user(); return response()->json(['success' => $user], $this-> successStatus); } }
How can I Solve this?
-
bgaze almost 5 yearsPlease note that in the code you provided, the routes point to "App\Http\Controllers\API\UserController" but that your controller is defined into the "App\Http\Controllers" namespace :-)