在Laravel中找不到路由,即使它存在

oxf4rvwz  于 2023-05-30  发布在  其他
关注(0)|答案(2)|浏览(358)

我尝试在Postman上运行API,收到以下错误:
“未找到方法”
我用laravel 5.7创建了控制器和它的路由,我还用“php artisan route:list”命令检查了路由是否存在!我可以找到那里的路线。它被列出
下面是控制器:

<?php

namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Response;
use GuzzleHttp\Client;
use Config;

class EletricityController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    
    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();

    }
}

下面是路由文件:

Route::get('eletricityToken','Api\EletricityController@eletricityToken')->name('eletricityToken');
c3frrgcw

c3frrgcw1#

您可以运行php artisan route:list来查看路由列表及其对应的控制器方法。
我记得在旧的Laravel项目中允许使用“Route::get('eletricityToken','Api\EletricityController@eletricityToken')->name('eletricityToken');”(可能是版本5)。
试试这个

use App\Http\Controllers\Api\EletricityController;

...

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

请注意在<?php在你的路由文件中。
最好的成功

vaqhlq81

vaqhlq812#

在路由文件(web.php/api.php)中,更改路由如下:

use App\Http\Controllers\Api\EletricityController;

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

相关问题