8

I'm new in Laravel. I've been followed few threads of how to load custom php library to Laravel 4,2 without dump autoload. So far I'm unable or I calling the function incorrect.

What I have so far is:

  1. I file autoload_classmap.php I've added my class

    'ImageResize' => $baseDir . '/app/libraries/ImageResize.php',

  2. In file autoload_static.php I've added

    'ImageResize' => __DIR__ . '/../..' . '/app/libraries/ImageResize.php',

Then in my controller in the function where I want to be shown this class I've tried like this

public function upload() {

$FmyFunction1 = new \ImageResize(); 
    return View::make('site.admin.upload', [
        'FmyFunction1' => $FmyFunction1
    ]);
}

Result is when I try to load /upload page I've got error:

'Class 'ImageResize' not found'

Is this error from wrong calling the class and/or error from not include correctly the class in Laravel at all? Can anyone help me?

ps. The reason I can't use dump autoload is because I have only ftp access to the host and I don't have SSH...

2
  • If you go into that file, is the class name exactly ImageResize and there is no namespace? It's possible the class is named something differently than the file. Commented Nov 14, 2016 at 15:00
  • Yes, the file is starting like: class ImageResize { .... } Commented Nov 14, 2016 at 15:09

2 Answers 2

7

You can add your namespace to the autoloader like this :

$loader = require 'vendor/autoload.php';
$loader->add('NameSpace', 'Path to directory'); // PSR-0 loading
$loader->addPsr4('NameSpace\\', 'Path to directory'); // PSR-4 loading

Doc : https://getcomposer.org/apidoc/1.0.0/Composer/Autoload/ClassLoader.html#method_add


The code needs to be added in the bootstrap.php file : you need to extend the base autoloader which is loaded with this line : require __DIR__.'/../vendor/autoload.php';

replace this line with $loader = require __DIR__.'/../vendor/autoload.php'; and add your custom namespace to the autoloader.

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry but in which file I should add this?
you can add it in bootstrap/autoload.php or in your index.php. It needs to be in a file loaded before your controller
I've added it to index.php in public dir and before $app->run(); .. stil class not found error.
Sorry for my late response.. I was a bit away. Just a question. Where is 'Path to directory' do I need to add full path there? like /var/wwwhtml/mysite/app/libraries/ImageResize.php
"path to directory" is the path to the base directory of your namespace. you can either use a full path or a relative path.
0

You can use PSR-4 autoloading.

With PSR-0/PSR-4, you don't have to dump-autoload when you add or update a class.

1 Comment

PSR-4 require dump auotload in order to load the new file in vendor/composer/autoload_psr4.php Also in video you send at the end he did it..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.