llagerlof / MoodleRest

MoodleRest is a PHP class to query Moodle REST webservices
MIT License
86 stars 21 forks source link

Add a namespace so that the class can be easily autoloaded by frameworks like Laravel #5

Open ssmusoke opened 3 years ago

llagerlof commented 3 years ago

Hi Stephen!

I just made a test using Laravel 8 and the framework autoloaded the class correctly.

My steps:

composer create-project laravel/laravel laravel-autoload
composer require llagerlof/moodlerest

Tha package was added into composer.json and composer.lock correctly.

Route::get('/', function () {
    $MoodleRest = new MoodleRest('http://127.0.0.1/moodle/webservice/rest/server.php', '8f12e614dae30735260a045313caa400');
    dd($MoodleRest);
    return view('welcome');
});

And it worked.

Did you ran composer dump-autoload inside your Laravel project? Sometimes this is necessary to Laravel find the new packages to autoload them.

And about the namespace, the reason I am avoiding it is because this class is aready being used by some people, and I don't want to disrupt something in their projects.

Art4 commented 3 weeks ago

And about the namespace, the reason I am avoiding it is because this class is aready being used by some people, and I don't want to disrupt something in their projects.

@llagerlof You might add a namespace to your class and use class_alias() to keep BC.

example:

// in MoodleRest.php

namespace Vendorname\Packagename;

class MoodleRest {
    //...
}

class_alias('Vendorname\Packagename\MoodleRest', 'MoodleRest');