PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 11, 2022

[FIXED] Class App\Http\Controllers\API\UserController does not exist

 March 11, 2022     laravel-5, oauth, oauth-2.0, passport-google-oauth, passport.js     No comments   

Issue

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?


Solution

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');


Answered By - bgaze
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing