我尝试在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');
2条答案
按热度按时间c3frrgcw1#
您可以运行
php artisan route:list
来查看路由列表及其对应的控制器方法。我记得在旧的Laravel项目中允许使用“
Route::get('eletricityToken','Api\EletricityController@eletricityToken')->name('eletricityToken');
”(可能是版本5)。试试这个
请注意在<?php在你的路由文件中。
最好的成功
vaqhlq812#
在路由文件(
web.php
/api.php
)中,更改路由如下: