2

I was reading some tutorials on creating custom classes for Laravel. I followed instructions and did exactly what tutorials say:

  1. Created new folder laravel/app/libraries/graphics/

  2. Edited laravel/app/start/global.php where I added:

    app_path().'/libraries/graphics',
    
  3. Created new file in laravel/app/libraries/graphics/ named Image.php with this code:

    <?php  namespace graphics/Image;
    
    class Image {
    
        public static function hello() {
    
        return 'Hello';
    
        }
    }
    
  4. Used composer dump-autload command

  5. Route::get('/' , function() { return Graphics\Image::hello(); } ); is returning error:

Use of undefined constant graphics - assumed 'graphics'

I also added "app/libraries/graphics/Image.php"line into composer.json autload section, which should not be neccessary. Why I am getting this error? Every tutorial shows the same procedure for this, but why it doesn't work?

Tomas Turan
  • 1,195
  • 3
  • 17
  • 27

3 Answers3

1

Shouldn't your namespace just be graphics? The current file creates graphics\Image\Image. Try removing Image from your namespace.

<?php  namespace graphics;

class Image {

    public static function hello() {

    return 'Hello';

    }
}
Jerodev
  • 32,252
  • 11
  • 87
  • 108
  • 1
    If I use "namespace graphics\Image;" and then "return graphics\Image\Image::hello();" it works. But if I use "namespace graphics;" and "return graphics\Image::hello();" then it doesn't work returning the same error. Why? – Tomas Turan Dec 09 '14 at 12:26
0

Have you tried using artisan dump-autoload instead?

It will clear all of Laravel's compiled code.

See here: What are differences between "php artisan dump-autoload" and "composer dump-autoload"

Community
  • 1
  • 1
iamyojimbo
  • 4,233
  • 6
  • 32
  • 39
0

You don't need to confusion for yourself. I'm resolve issue into Laravel 5. You don't need to add "app/libraries/graphics/Image.php"line into composer.json autload section because By default, app directory is namespaced under App and is autoloaded by Composer using the PSR-4 autoloading standard.

<?php 
namespace App\libraries\graphics;
class Image {
     public static function hello() {
         return 'Hello';
     }
}

and now use your Image Class from your route.

Route::get('graphics',function(){
    echo \App\libraries\graphics\Image::hello();
});
Dhaval Tailored
  • 386
  • 2
  • 6