Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading custom php file in Laravel without composer dump-autoload

Tags:

php

laravel

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...

like image 955
Jane Avatar asked Nov 14 '16 14:11

Jane


People also ask

What is the purpose of composer dump-autoload?

Composer dump-autoload: The composer dump-autoload will not download any new thing, all it does is looking for all the classes and files it needs to include again.

What is autoloading in laravel?

Auto-Loading allows you to load class files when they are needed without explicitly loading or including them. This gives you ease in running your application by loading those files automatically which are needed every time. Laravel is built to work with Composer.

What is PHP artisan dump-autoload?

If you need to regenerate your package's autoload files, you may use the php artisan dump-autoload command. This command will regenerate the autoload files for your root project, as well as any workbenches you have created.

How do I create a composer autoload?

The simplest way is to autoload each class separately. For this purpose, all we need to do is define the array of paths to the classes that we want to autoload in the composer. json file.


1 Answers

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.

like image 94
ᴄʀᴏᴢᴇᴛ Avatar answered Oct 23 '22 11:10

ᴄʀᴏᴢᴇᴛ