skip to Main Content

I am trying to run an api on postman, I am getting the following error:

"Method not found"

I created the controller and its route using laravel 5.7, I also checked whether the route exists using "php artisan route:list" command! I can get the route there. it is listed

below is the controller:

<?php

namespace AppHttpControllersApi;
use IlluminateHttpRequest;
use AppHttpControllersController;
use GuzzleHttpExceptionGuzzleException;
use IlluminateHttpResponse;
use GuzzleHttpClient;
use Config;


class EletricityController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */
    
    public function eletricityToken(Request $request){
      
        $client = new Client();
        $URL = Config('api.authUrl');
        $headers = [
        'Authorization' => 'Bearer <token>'
        ];
        $options = [
        'form_params' => [
        'client_id' => Config('api.client_id'),
        'client_secret' => Config('api.client_secret'),
        'username' => Config('api.username'),
        'password' => Config('api.password'),
        'grant_type' => 'password',
        'scope' => 'read write openid'
        ]];
        $request = new Request('POST', $URL, $headers);
        $res = $client->sendAsync($request, $options)->wait();
        echo $res->getBody();

    }
}

below is the route file:

Route::get('eletricityToken','ApiEletricityController@eletricityToken')->name('eletricityToken');

2

Answers


  1. On routes file(web.php/api.php), change the route like:

    use AppHttpControllersApiEletricityController;
    
    Route::get('/eletricityToken', [EletricityController::class, 'eletricityToken'])->name('eletricityToken');
    
    Login or Signup to reply.
  2. You can run php artisan route:list to see the list of routes and their corresponding controller methods.

    I can remember that "Route::get('eletricityToken','ApiEletricityController@eletricityToken')->name('eletricityToken');" was allowed in old Laravel projects (version 5, maybe).

    Try this

    use AppHttpControllersApiEletricityController;
    
    ...
    
    Route::get('eletricityToken',[EletricityController::class,'eletricityToken'])->name('eletricityToken');
    

    Please note to add use AppHttpControllersApiEletricityController; immediately after the <?php in your route file.

    Best of success

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search