首页 > 解决方案 > Laravel:无法使用自动加载功能

问题描述

我有一个关于 Laravel 自动加载的问题。

我正在尝试使用 API 来翻译网站。我在控制器中输入了提供商提供的代码,我收到了以下错误消息。

require(vendor\autoload.php):打开流失败:没有这样的文件或目录

这是代码。

require 'vendor\autoload.php';    <=★Here★

session_start();

define('URL', '');
define('KEY', '');
define('SECRET', '');
define('NAME', '');

$api_name = '';
$api_param = '';

$provider = new \League\OAuth2\Client\Provider\GenericProvider(
    [
        'clientId'                => KEY,                            
        'clientSecret'            => SECRET,                         
        'redirectUri'             => '',                             
        'urlAuthorize'            => '',                             
        'urlAccessToken'          => URL . '/oauth2/token.php',      
        'urlResourceOwnerDetails' => '',
    ],
);

try {

    // Try to get an access token using the authorization code grant.
    $accessToken = $provider->getAccessToken('client_credentials');

    // The provider provides a way to get an authenticated API request for
    // the service, using the access token; it returns an object conforming
    // to Psr\Http\Message\RequestInterface.

    $params = array(
        'access_token' => $accessToken->getToken(),                   
        'key' => KEY,                                                 
        'api_name' => $api_name,
        'api_param' => $api_param,
        'name' => NAME,                                               
        'type' => 'xml',                                              
        'xxx' => 'xxx',                                               
    );

    $request = $provider->getAuthenticatedRequest(
        'POST',
        URL . '/api/?' . http_build_query($params),                    
        $accessToken,
    );

    $response = $provider->getResponse($request);
    $data = $response->getBody()->getContents();
    print_r($data);

} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {

    // Failed to get the access token or user details.
    exit($e->getMessage());

}

我检查了错误消息中指出的文件,实际上 autoload.php 文件位于供应商文件夹中。我做了一些研究,发现一些网站说我应该在 composer.json 中添加一个目录,但我不太确定我应该做什么。

如果你能告诉我我做错了什么或我应该做什么,我将不胜感激。任何帮助将不胜感激,因为我尝试了多种方法都没有成功。

先感谢您。

标签: laravelautoload

解决方案


推荐阅读