'Trying to override the method of laravel passport package by the help autoload psr-4 but does not get expected result

Composer.json

"autoload": {
        "exclude-from-classmap": [
            "vendor\\laravel\\passport\\src\\Http\\Controllers\\AccessTokenController.php"
        ],
        "psr-4": {
            "App\\": "app/",
            "Laravel\\Passport\\": "app/Override/"  //tried "Passport//" only also
        }
    },

I have file AccessTokenController.php which lie in vendor\laravel\passport\src\Http\Controllers\ and i create Override folder inside App directory on where i copied that file and make changes in code And finally when i do :

composer dump-autoload

i get error as :Class Laravel\Passport\Http\Controllers\AccessTokenController located in ./app/Override/AccessTokenController.php does not comply with psr-4 autoloading standard. Skipping. seems problem with namespace which i couldn't figure out though trying some of the ways .. Anyone can help me ?



Solution 1:[1]

I would simply override the route from within the app as mentioned here. No need for the approach you mentioned above.

However, if you want to keep going this route you can try the following.

"psr-4": {
    "App\\": "app/",
}
"exclude-from-classmap": [
    "Laravel\\Passport\\Http\\Controllers\\AccessTokenController"
],
"files": [ 
   "app/Override/AccessTokenController.php"
], 

Solution 2:[2]

I solved it by including complete namespace of AccessTokenController.php.so final code would look like this:

"autoload": {
        "exclude-from-classmap": [
            "vendor\\laravel\\passport\\src\\Http\\Controllers\\AccessTokenController.php"
        ],
        "psr-4": {
            "App\\": "app/",
            "Laravel\\Passport\\Http\\Controllers\\":"app/Override/"
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Gunaraj Khatri