Class /vendor/autoload not found

Jyotishgher Astrology
By -
0

Composer Autoloading classes not found

In PHP, both __DIR__ and __DIR__ . '/..' are used to specify file paths relative to the current script's directory. What is Composer autoloading? Now move on to an important Composer feature: autoload. Autoloading is a way to automatically include the necessary files when a class is used in an application, without having to manually include or require each fileHowever, they differ in their reference point.

autoload not found

1. __DIR__:

  • Reference Point: The directory containing the current script.
  • Usage:
    • To include files that are in the same directory as the current script.
    • To reference files within subdirectories of the current directory.
require __DIR__ . '/vendor/autoload.php';

2. __DIR__ . '/..':

  • Reference Point: The parent directory of the current script.
  • Usage:
    • To include files that are in the parent directory of the current script.
    • To reference files within subdirectories of the parent directory.

require __DIR__ . '/../vendor/autoload.php';
Example 
<?php


include_once($_SERVER['DOCUMENT_ROOT'] . "/your path of connection");
require __DIR__ . '/../vendor/autoload.php';


use \Firebase\JWT\JWT;
use \Firebase\JWT\Key;

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$headers = apache_request_headers();

if (isset($headers['Authorization'])) {
    $jwt = str_replace("Bearer ", "", $headers['Authorization']);

    try {

 

$secret_key define at a comman place 
        $decoded = JWT::decode($jwt, new Key($secret_key, 'HS256'));
        
        $sql = oci_parse($conn, "SELECT * from dual");
        oci_execute($sql);

        $result = array();
        while ($r = oci_fetch_array($sql, OCI_ASSOC + OCI_RETURN_NULLS)) {
            array_push($result, array('Srno' => $r['Srno'], 'dName' => $r['NAME']));
        }

        echo json_encode(["status" => 200, "message" => "Access granted", "data" => ["deptList" => $result]]);
    } catch (\Firebase\JWT\ExpiredException $e) {
        echo json_encode(["status" => 401, "message" => "Token expired", "error" => $e->getMessage()]);
    } catch (\Firebase\JWT\SignatureInvalidException $e) {
        echo json_encode(["status" => 401, "message" => "Invalid signature", "error" => $e->getMessage()]);
    } catch (Exception $e) {
        echo json_encode(["status" => 401, "message" => "Access denied", "error" => $e->getMessage()]);
    }
} else {
    echo json_encode(["status" => 401, "message" => "Token not provided"]);
 
?>

Common Use Case: Autoloading with Composer

The most common use case for __DIR__ . '/../vendor/autoload.php' is to include Composer's autoloader. Composer, a dependency manager for PHP, generates an autoloader file in the vendor directory. This autoloader allows you to use classes from installed packages without explicitly requiring them.

In summary:

  • Use __DIR__ when you want to reference files within the same directory or its subdirectories.
  • Use __DIR__ . '/..' when you want to reference files in the parent directory or its subdirectories.

By understanding these distinctions, you can effectively manage file paths and dependencies in your PHP projects.

Conclusions

The "Class not found" error in PHP occurs when you attempt to use a class that PHP cannot find or load. This typically happens when you haven't included the file that contains the class definition, or there is a typo in the class name.

Tags:

Post a Comment

0Comments

Post a Comment (0)