Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ajorgenson's avatar

Issue with PSR-4 Autoloading

I'm attempting to autoload a class I found online for utilizing Google's URL shortener API to make my life easier.

I'm just trying to get the autoload working, in which I'm failing horribly.

I have ran composer autoload-dump -o .

My autoload json:

    "autoload": {
        "classmap": [
            "database" 
        ],
        "psr-4": {
            "App\\": "app/",
            "Library\\": "App/Library"
        }
    },

The class I'm trying to load:

<?php 
namespace Library\GoogleUrlApi;

/*
    https://davidwalsh.name/google-url
*/

// Declare the class
class GoogleUrlApi {
    
    public function __construct() {
        dd('Test');
    }
    
    // Constructor
    function GoogleURLAPI($key,$apiURL = 'https://www.googleapis.com/urlshortener/v1/url') {
        // Keep the API Url
        $this->apiURL = $apiURL.'?key='.$key;
    }
    
    // Shorten a URL
    function shorten($url) {
        // Send information along
        $response = $this->send($url);
        // Return the result
        return isset($response['id']) ? $response['id'] : false;
    }
    
    // Expand a URL
    function expand($url) {
        // Send information along
        $response = $this->send($url,false);
        // Return the result
        return isset($response['longUrl']) ? $response['longUrl'] : false;
    }
    
    // Send information to Google
    function send($url,$shorten = true) {
        // Create cURL
        $ch = curl_init();
        // If we're shortening a URL...
        if($shorten) {
            curl_setopt($ch,CURLOPT_URL,$this->apiURL);
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode(array("longUrl"=>$url)));
            curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-Type: application/json"));
        }
        else {
            curl_setopt($ch,CURLOPT_URL,$this->apiURL.'&shortUrl='.$url);
        }
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        // Execute the post
        $result = curl_exec($ch);
        // Close the connection
        curl_close($ch);
        // Return the result
        return json_decode($result,true);
    }       
}

A picture of directory structure: http://i.imgur.com/C00q6mu.png

Attempted utilization:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;




class BeaconController extends Controller
{
    /*
        Controls the creation of beacon URLS
    */
    
    public function create($building_id)
    {
        $key = "";
        new \Library\GoogleUrlApi;
        //$googer = $api->GoogleUrlApi($key);
        //var_dump($response);
    }
}
0 likes
2 replies
EmilMoe's avatar

You can use ~ ~ ~ ~ before and after code (without the spaces) then it will be nice formatted :-)


I have ran composer autoload-dump -o .

My autoload json:
    "autoload": {
        "classmap": [
            "database" 
        ],
        "psr-4": {
            "App\\": "app/",
            "Library\\": "App/Library"
        }
    },

The class I'm trying to load:
http://pastebin.com/xdJdCCdc

A picture of directory structure:
http://i.imgur.com/C00q6mu.png

Attempted utilization:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;




class BeaconController extends Controller
{
    /*
        Controls the creation of beacon URLS
    */
    
    public function create($building_id)
    {
        $key = "";
        new \Library\GoogleUrlApi;
        //$googer = $api->GoogleUrlApi($key);
        //var_dump($response);
    }
}
1 like
moharrum's avatar
moharrum
Best Answer
Level 6

composer.json :

    "autoload": {
        "classmap": [
            "database" 
        ],
        "psr-4": {
            "App\\": "app/",
            "Library\\": "app/Library"
        }
    },

Change App to app, you also need to change the namespace form namespace Library\GoogleUrlApi; to namespace Library;. Don't forget to run composer du.

2 likes

Please or to participate in this conversation.